将gtest与cmake / ctest一起使用时未找到测试 [英] No tests found when using gtest with cmake/ctest
问题描述
我有一个结构如下的项目:
I have a project with the following structure:
linalg
├── build
├── CMakeLists.txt
├── docs
│ └── Doxyfile
├── include
│ └── linalg
│ └── vector3.hpp
├── src
│ ├── CMakeLists.txt
│ └── linalg
│ └── vector3.cpp
└── test
├── CMakeLists.txt
└── linalg
└── test_vector3.cpp
文件test_vector3.cpp是一个gtest单元测试文件,它提供了两个简单的测试。顶级CMakeLists.txt只需设置包含内容并添加src和测试子目录:
The file test_vector3.cpp is a gtest unit test file which provides two simple tests. The top level CMakeLists.txt simply sets up the includes and adds the src and test subdirectories:
cmake_minimum_required(VERSION 2.8)
project(linalg)
include_directories(include)
add_subdirectory(src)
add_subdirectory(test)
src / CMakeLists.txt文件将vector3.cpp编译为静态库:
The src/CMakeLists.txt file compiles vector3.cpp into a static library:
cmake_minimum_required(VERSION 2.8)
add_library(linalg linalg/vector3.cpp)
test / CMakeLists.txt文件基于/usr/share/cmake-2.8/Modules/FindGTest.cmake中提供的示例:
The test/CMakeLists.txt file is based on the example provided in /usr/share/cmake-2.8/Modules/FindGTest.cmake:
cmake_minimum_required(VERSION 2.8)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(test_vector3 linalg/test_vector3.cpp)
target_link_libraries(test_vector3 linalg ${GTEST_BOTH_LIBRARIES} pthread)
add_test(test_vector3 test_vector3)
然后运行以下命令:
cd build
cmake ..
make
我正确编译了liblinalg.a库以构建/ src,并且我正确编译了test_vector3可执行文件以构建/测试。我可以运行test_vector3可执行文件,并从googletest获得输出,说所有测试都已通过,但是如果我运行 make test
,我什么也不会输出,如果我运行 ctest ..
我收到一条消息:
I get the liblinalg.a library compiled correctly in to build/src and I get the test_vector3 executable compiled correctly in to build/test. I can run the test_vector3 executable and I get the output from googletest saying that all tests have passed, however if I run make test
I get no output whatsoever and if I run ctest ..
I get a message saying:
Test project /home/ryan/GitHub/linalg/build
No tests were found!!!
我缺少什么吗?还是我只是误解了ctest与gtest一起工作的方式?
Is there something I am missing? Or have I just misunderstood how ctest works with gtest?
推荐答案
问题的症结在于 enable_testing在这种情况下,应从顶级CMakeLists.txt调用
。添加 include(CTest)
到顶级CMakeLists.txt应该可以为您解决此问题。
The crux of the problem is that enable_testing
should be called from your top-level CMakeLists.txt in this case. Adding include(CTest)
to your top-level CMakeLists.txt should fix this for you.
这将允许您删除 enable_testing
调用,因为CTest子模块在内部调用 enable_testing
。
This would allow you to remove the enable_testing
call in test/CMakeLists.txt, since the CTest submodule calls enable_testing
internally.
这篇关于将gtest与cmake / ctest一起使用时未找到测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!