第二章 UT单元测试——GTest框架实例
系列文章目录
第一章 UT单元测试——GoogleTest通用构建说明
第二章 UT单元测试——GTest框架实例
文章目录
- 系列文章目录
- 前言
- 一、环境配置
- 二、使用步骤
- 1.添加待测源码
- 2.修改cmake文件
- 3.添加自动化脚本
- 4.运行GTest实例
- 5.运行结果
- 总结
前言
第一章介绍了GTest通用构建说明,本文就按照说明中推荐的方法搭建一套GTest框架实例吧。
提示:以下是本篇文章正文内容,下面案例可供参考
一、环境配置
本文使用Linux环境。(后续补充)
二、使用步骤
1.添加待测源码
下载官方示例代码,或者导入已有代码。
example.cpp(示例):
#include <stdio.h>int main(){ printf("Hello gtest\n");}
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 timeconfigure_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 Windowsset(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. Egadd_executable(example example.cpp)target_link_libraries(example gtest_main)add_test(NAME example_test COMMAND example)# batch compile samplesfile(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 srcsforeach(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 srcsforeach(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 buildcd buildcmake ..make -j4# test samples../test_samples.sh# test example../test_example.sh
test_samples.sh:
# get this script pathcurPath=$(dirname $(readlink -f "$0"))echo "entering build..."cd $curPath/buildpwd# execute sample programsfor file in `ls -dv sample[0-9]*`doecho "testing $file..."./$filedone
test_example.sh:
# get this script pathcurPath=$(dirname $(readlink -f "$0"))echo "entering build..."cd $curPath/buildpwd# execute sample programsfor file in `ls -dv example`doecho "testing $file..."./$filedone
4.运行GTest实例
README.md:
A simple framework implementation of GoogleTest.Linux command:1. chmod x setup.sh test_samples.sh test_example.sh2. ./setup
5.运行结果
测试结果待补充。
总结
以上就是今天要讲的内容,本文仅仅简单介绍了GTest框架的使用,而GTest框架提供了大量能使我们快速便捷地模拟和测试的工具和方法。
赞 (0)