使用CMake的Google C ++ Testing Framework的最简单示例

时间:2011-05-05 15:37:36

标签: c++ unit-testing cmake googletest

2 个答案:

答案 0 :(得分:25)

启用CMake的内置测试子系统:

# For make-based builds, defines make target named test.
# For Visual Studio builds, defines Visual Studio project named RUN_TESTS.
enable_testing()

编译将运行单元测试的可执行文件并将其与gtest和gtest_main链接:

add_executable(runUnitTests
    project1_unittests.cpp
)
target_link_libraries(runUnitTests gtest gtest_main)

添加运行此可执行文件的测试:

add_test(
    NAME runUnitTests
    COMMAND runUnitTests
)

答案 1 :(得分:0)

这是一个最简单的,

1.创建一个简单的源文件,

$ cat simplegtest.cpp 

#include<gtest/gtest.h>
TEST(Mytest, failing_test){
    EXPECT_TRUE(false);
}

2.使用下面的命令编译它,

$ LDLIBS="-lgtest_main -lgtest" make simplegtest
g++     simplegtest.cpp  -lgtest_main -lgtest -o simplegtest

3.使用以下命令执行测试可执行文件,

$ ./simplegtest 
Running main() from /home/prashant/work/thirdparty/googletest-release-1.8.1/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Mytest
[ RUN      ] Mytest.failing_test
simplegtest.cpp:4: Failure
Value of: false
  Actual: false
Expected: true
[  FAILED  ] Mytest.failing_test (0 ms)
[----------] 1 test from Mytest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Mytest.failing_test

 1 FAILED TEST