概述
系列文章目录
第一章 UT单元测试——GoogleTest通用构建说明
第二章 UT单元测试——GTest框架实例
文章目录
- 系列文章目录
- 前言
- 一、环境配置
- 二、使用步骤
- 1.添加待测源码
- 2.修改cmake文件
- 3.添加自动化脚本
- 4.运行GTest实例
- 5.运行结果
- 总结
前言
第一章介绍了GTest通用构建说明,本文就按照说明中推荐的方法搭建一套GTest框架实例吧。
提示:以下是本篇文章正文内容,下面案例可供参考
一、环境配置
本文使用Linux环境。(后续补充)
二、使用步骤
1.添加待测源码
下载官方示例代码,或者导入已有代码。
example.cpp(示例):
#include <stdio.h>
int main()
{
printf("Hello gtestn");
}
2.修改cmake文件
CMakelists.txt.in:
cmake_minimum_required(VERSION 2.8.12)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY
https://github.com/google/googletest.git
GIT_TAG
master
SOURCE_DIR
"${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR
"${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND
""
INSTALL_COMMAND
""
TEST_COMMAND
""
)
CMakelists.txt:
cmake_minimum_required(VERSION 3.16)
project(GoogleTestExpl)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(example example.cpp)
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)
# batch compile samples
file(GLOB_RECURSE utfiles ${CMAKE_CURRENT_SOURCE_DIR} samples/*_unittest.cc)
file(GLOB_RECURSE srcfiles ${CMAKE_CURRENT_SOURCE_DIR} samples/*[0-9].cc)
list(LENGTH utfiles utfileslen)
message(STATUS "utfileslen=${utfileslen}")
list(LENGTH srcfiles srcfileslen)
message(STATUS "srcfileslen=${srcfileslen}")
# get all srcs
foreach(srcfile IN LISTS srcfiles)
file(RELATIVE_PATH srcfilename ${CMAKE_CURRENT_SOURCE_DIR}/samples ${srcfile})
list(APPEND srcfilenames ${srcfilename})
endforeach(srcfile IN LISTS srcfiles)
message(STATUS "srcfilenames=${srcfilenames}")
# compile utfile with srcs
foreach(utfile IN LISTS utfiles)
file(RELATIVE_PATH utfilename ${CMAKE_CURRENT_SOURCE_DIR}/samples ${utfile})
list(FIND utfiles ${utfile} i)
# get filename without extension
get_filename_component(targetname ${utfile} NAME_WE)
add_executable(${targetname} ${utfile} ${srcfiles})
target_link_libraries (${targetname} gtest_main)
add_test(NAME ${targetname}_test COMMAND ${targetname})
message(STATUS "Recipe ${i}: test=${targetname}_test, target=${targetname}, ut=${utfilename}, src=${srcfilenames}, link=gtest_main")
endforeach(utfile IN LISTS utfiles)
3.添加自动化脚本
setup.sh:
mkdir build
cd build
cmake ..
make -j4
# test samples
../test_samples.sh
# test example
../test_example.sh
test_samples.sh:
# get this script path
curPath=$(dirname $(readlink -f "$0"))
echo "entering build..."
cd $curPath/build
pwd
# execute sample programs
for file in `ls -dv sample[0-9]*`
do
echo "testing $file..."
./$file
done
test_example.sh:
# get this script path
curPath=$(dirname $(readlink -f "$0"))
echo "entering build..."
cd $curPath/build
pwd
# execute sample programs
for file in `ls -dv example`
do
echo "testing $file..."
./$file
done
4.运行GTest实例
README.md:
A simple framework implementation of GoogleTest.
Linux command:
1. chmod +x setup.sh test_samples.sh test_example.sh
2. ./setup
5.运行结果
测试结果待补充。
总结
以上就是今天要讲的内容,本文仅仅简单介绍了GTest框架的使用,而GTest框架提供了大量能使我们快速便捷地模拟和测试的工具和方法。
最后
以上就是苹果红牛为你收集整理的第二章 UT单元测试——GTest框架实例系列文章目录前言一、环境配置二、使用步骤总结的全部内容,希望文章能够帮你解决第二章 UT单元测试——GTest框架实例系列文章目录前言一、环境配置二、使用步骤总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复