本指南将向您展示如何通过Google的测试框架使用CMake和单元测试来设置新的C ++项目。 通过此设置,您可以立即开始使用C ++进行测试驱动的开发。 弄清楚如何将gtest添加到现有项目并开始在旧的(现有)代码库上进行TDD也很简单。

环境:win10 子系统linux ubuntu18.04

确保已经安装了cmake(3.10以上),g++,gcc等基本环境。

最后的效果图:

从github上将 google test 源码clone下来

git clone https://github.com/google/googletest/

gtest 项目里面包含了CMakeLists.txt,所以集成到自己的项目里面是比较简单的(如果你懂cmake的话,如果不懂的话就按照本指南做吧。

IndexProject
├── CMakeLists.txt
├── build
├── lib
│   └── googletest
├── src
│   ├── CMakeLists.txt
│   ├── Formula.cpp
│   ├── Formula.h
│   └── main.cpp
└── tst
    ├── CMakeLists.txt
    ├── Formula-test.cpp
    └── main.cpp

下面这个命令是创建这个目录结构

mkdir -p IndexProject/{build,lib,src,tst}

将clone下来的gooletest 文件夹放到项目的lib文件夹下。

大部分的个人的简单项目用以上目录结构应该都没问题,所以这个相当于一个模板吧,供自己以后以及其它菜鸟参考。最好的方式当然是学习CMake,然后自己随心所欲想怎么搞怎么搞,可以CMake想搞明白还挺花时间的,对于时间紧迫的人这个模板是个不错的选择。

CMakeLists.txt

生成target有两种方式,一种直接将文件名一个个列出来,这样的好处是不需要编译不必要的文件。

add_executable(ExampleProject main.cpp file1.cpp file1.h)

还有一种是将所有的文件全编译然后生成执行文件,这样的好处是添加文件后不需要修改CMakeLists.txt就可以直接运行。

file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.h *.cpp)

根目录下的CMakeLists.txt

# cmake version
cmake_minimum_required(VERSION 3.10)
# project name
project(IndexProject)
#采用c++14标准
set(CMAKE_CXX_STANDARD 14)
include_directories(src)
add_subdirectory(src)
add_subdirectory(tst)
add_subdirectory(lib/googletest)

这里的include_directories(src)是为了让src下的头文件全局可见。

src 目录下的CMakeLists.txt

#设置 BINARY 为项目名IndexProject
set(BINARY ${CMAKE_PROJECT_NAME})
# add_executable(ExampleProject main.cpp file1.cpp file1.h)
file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.h *.cpp)
set(SOURCES ${SOURCES})
add_executable(${BINARY}_run ${SOURCES})
# 为了让单元测试的时候src下的代码能被作为静态链接库使用
add_library(${BINARY}_lib STATIC ${SOURCES})

tst 目录下的CMakeLists.txt

set(BINARY ${CMAKE_PROJECT_NAME}_tst)
file(GLOB_RECURSE TEST_SOURCES LIST_DIRECTORIES false *.h *.cpp)
set(SOURCES ${TEST_SOURCES})
add_executable(${BINARY} ${TEST_SOURCES})
add_test(NAME ${BINARY} COMMAND ${BINARY})
# 链接src生成的lib库和gtest库
target_link_libraries(${BINARY} PUBLIC ${CMAKE_PROJECT_NAME}_lib gtest)

添加源代码

src/main.cpp:

#include <iostream>
#include "Formula.h"
int main() {
    std::cout << "Bla: " << Formula::bla(2) << std::endl;
    return 0;

src/Formula.h

#ifndef EXAMPLEPROJECT_FORMULA_H
#define EXAMPLEPROJECT_FORMULA_H
class Formula {
public:
    static int bla(int arg1);
#endif //EXAMPLEPROJECT_FORMULA_H

src/Formula.cpp:

#include "Formula.h"
int Formula::bla(int arg1) {
    return arg1 * 2;

添加测试代码

tst/main.cpp

#include "gtest/gtest.h"
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();

上面那个文件会执行所有tst目录下的测试文件的所有测试用例,这里只添加一个。

tst/Formula-test.cpp

#include "gtest/gtest.h"
#include "Formula.h"
TEST(blaTest, test1) {
    //arrange
    //act
    //assert
    EXPECT_EQ (Formula::bla (0),  0); //通过
    EXPECT_EQ (Formula::bla (2), 4); //通过
    EXPECT_EQ (Formula::bla (4), 6); //不通过
cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /usr/bin/python3.6 (found version "3.6.9")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /d/code/IndexProject/build

然后使用 make 编译生成我们需要的东西,可以使用

make all

生成所有的。

Scanning dependencies of target IndexProject_lib
[  5%] Building CXX object src/CMakeFiles/IndexProject_lib.dir/Formula.cpp.o
[ 11%] Building CXX object src/CMakeFiles/IndexProject_lib.dir/main.cpp.o
[ 17%] Linking CXX static library libIndexProject_lib.a
[ 17%] Built target IndexProject_lib
Scanning dependencies of target IndexProject_run
[ 23%] Building CXX object src/CMakeFiles/IndexProject_run.dir/Formula.cpp.o
[ 29%] Building CXX object src/CMakeFiles/IndexProject_run.dir/main.cpp.o
[ 35%] Linking CXX executable IndexProject_run
[ 35%] Built target IndexProject_run
Scanning dependencies of target gtest
[ 41%] Building CXX object lib/googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 47%] Linking CXX static library ../../libgtestd.a
[ 47%] Built target gtest
Scanning dependencies of target IndexProject_tst
[ 52%] Building CXX object tst/CMakeFiles/IndexProject_tst.dir/Formula-test.cpp.o
[ 58%] Building CXX object tst/CMakeFiles/IndexProject_tst.dir/main.cpp.o
[ 64%] Linking CXX executable IndexProject_tst
[ 64%] Built target IndexProject_tst
Scanning dependencies of target gmock
[ 70%] Building CXX object lib/googletest/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 76%] Linking CXX static library ../../libgmockd.a
[ 76%] Built target gmock
Scanning dependencies of target gmock_main
[ 82%] Building CXX object lib/googletest/googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 88%] Linking CXX static library ../../libgmock_maind.a
[ 88%] Built target gmock_main
Scanning dependencies of target gtest_main
[ 94%] Building CXX object lib/googletest/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library ../../libgtest_maind.a
[100%] Built target gtest_main

这里还生成了gmock,这是googletest下面的另一个项目,如果不想生成它们,也可以使用以下命令生成我们想要的:

make IndexProject_tst;make IndexProject_run
[ 12%] Building CXX object src/CMakeFiles/IndexProject_lib.dir/Formula.cpp.o
[ 25%] Building CXX object src/CMakeFiles/IndexProject_lib.dir/main.cpp.o
[ 37%] Linking CXX static library libIndexProject_lib.a
[ 37%] Built target IndexProject_lib
[ 50%] Building CXX object lib/googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 62%] Linking CXX static library ../../libgtestd.a
[ 62%] Built target gtest
[ 75%] Building CXX object tst/CMakeFiles/IndexProject_tst.dir/Formula-test.cpp.o
[ 87%] Building CXX object tst/CMakeFiles/IndexProject_tst.dir/main.cpp.o
[100%] Linking CXX executable IndexProject_tst
[100%] Built target IndexProject_tst
[ 33%] Building CXX object src/CMakeFiles/IndexProject_run.dir/Formula.cpp.o
[ 66%] Building CXX object src/CMakeFiles/IndexProject_run.dir/main.cpp.o
[100%] Linking CXX executable IndexProject_run
[100%] Built target IndexProject_run

找到我们生成的可执行文件

$ find . -executable -type f -name "IndexProject*"
./src/IndexProject_run
./tst/IndexProject_tst
$ ./src/IndexProject_run;./tst/IndexProject_tst
Bla: 4
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from blaTest
[ RUN      ] blaTest.test1
/d/code/IndexProject/tst/Formula-test.cpp:9: Failure
Expected equality of these values:
  Formula::bla(4)
    Which is: 8
[  FAILED  ] blaTest.test1 (1 ms)
[----------] 1 test from blaTest (2 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (3 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] blaTest.test1
 1 FAILED TEST

这篇指南主要参考一个国外的博客,如果想看原版内容,点击这里,找了好久才找到能看懂的博客,cmake看了大半天还是没太搞明白,只能看懂这种傻瓜式的教程了哈哈。以上博客如果记载有错漏之处请私信或者直接查看原文。本人也只是按照那个博客自己试了一遍。

  • 私信