我一直在查看CMake的示例,以帮助我使用其测试文件夹来构建项目。主要问题是我必须将测试文件夹中的所有test /。cpp *文件都包含在tests / main.cpp中才能运行测试。我认为我应该在test / CMakeLists.txt中的add_test
调用中包含测试。以下是我当前的CMakeLists文件:
enable_testing()
find_package(需要GTest)
add_executable(test main.cpp)
target_include_directories(test PUBLIC $ {CMAKE_CURRENT_SOURCE_DIR} /../ include)
target_link_libraries(test MainProjectLib $ {GTEST_LIBRARIES})
而我的main.cpp如下。
#include "file_in_test1.cpp"
#include "file_in_test2.cpp"
#include <gtest/gtest.h>
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我阅读了一些使用标准CTest库进行单元测试的示例,但我使用的是Google Test。 我希望能够运行cmake
,make
,然后运行./test
来运行file_in_test1.cpp和file_in_test2.cpp中找到的所有测试方法,而无需直接导入将这些文件放入test / main.cpp。我必须对CMakeLists.txt进行哪些添加?
您可以在末尾添加的一行将是:
gtest_discover_tests(test)
这是gtest_add_tests
的替代品,然后在构建所有内容之后,您只需运行ctest
,它将为您提供刚刚运行的测试的简要摘要。您可以阅读有关此选项here的更多信息。