在文档中,它们只编译一个可能包含所有测试的文件test.cpp。我希望将我的单个测试从包含#define CATCH_CONFIG_MAIN的文件中分离出来,比如所以。
如果我有一个包含#define CATCH_CONFIG_MAIN的文件#define CATCH_CONFIG_MAIN和一个单独的测试文件simple_test.cpp,我已经设法以这种方式生成了一个包含simple_test.cpp测试的可执行文件:
find_package(Catch2 REQUIRED)
add_executable(tests test.cpp simple_test.cpp)
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)但是,这是一种可接受的产生可执行文件的方法吗?从不同的教程中,如果我有更多的测试,我应该能够创建一个测试源库,并将它们链接到test.cpp以生成可执行文件:
find_package(Catch2 REQUIRED)
add_library(test_sources simple_test.cpp another_test.cpp)
target_link_libraries(test_sources Catch2::Catch2)
add_executable(tests test.cpp)
target_link_libraries(tests test_sources)
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)但是当我尝试这个时,我得到了一个CMake警告Test executable ... contains no tests!。
总之,我应该做一个测试库吗?如果是这样,我如何使它包含我的测试。否则,将我的新test.cpp文件添加到add_executable函数中正确吗?
发布于 2020-12-29 23:14:52
如何使用Catch2和CMake添加单独的测试文件?
使用对象库或使用--Wl,--whole-archive。链接器在链接时从静态库中删除未引用的符号,因此测试不在最终的可执行文件中。
你能举个例子CMakeLists.txt吗?
喜欢
find_package(Catch2 REQUIRED)
add_library(test_sources OBJECT simple_test.cpp another_test.cpp)
target_link_libraries(test_sources Catch2::Catch2)
add_executable(tests test.cpp)
target_link_libraries(tests test_sources)
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)或
find_package(Catch2 REQUIRED)
add_library(test_sources simple_test.cpp another_test.cpp)
target_link_libraries(test_sources Catch2::Catch2)
add_executable(tests test.cpp)
target_link_libraries(tests -Wl,--whole-archive test_sources -Wl,--no-whole-archive)
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)https://stackoverflow.com/questions/65499778
复制相似问题