Simplest example of using Google C++ Testing Framework with CMake

99封情书 提交于 2019-12-03 01:43:26

问题


I have a very simple C++ library (one header file, one .cpp file). I want to write unit tests for this project using the Google C++ Testing Framework.

Here is the directory structure:

~/project1
 |
 |-- project1.cpp
 |-- project1.h
 |-- project1_unittests.cpp
 \-- CMakeLists.txt

I do not plan to write my own main() function. I want to link with gtest_main as mentioned in the primer. What should CMakeLists.txt contain?


回答1:


Enable CMake's built-in testing subsystem:

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

Compile an executable that will run your unit tests and link it with gtest and gtest_main:

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

Add a test which runs this executable:

add_test(
    NAME runUnitTests
    COMMAND runUnitTests
)


来源:https://stackoverflow.com/questions/5900447/simplest-example-of-using-google-c-testing-framework-with-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!