No tests found when using gtest with cmake/ctest

别来无恙 提交于 2019-11-30 08:25:07

问题


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

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)

The src/CMakeLists.txt file compiles vector3.cpp into a static library:

cmake_minimum_required(VERSION 2.8)

add_library(linalg linalg/vector3.cpp)

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)

I then run the following:

cd build
cmake ..
make

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!!!

Is there something I am missing? Or have I just misunderstood how ctest works with gtest?


回答1:


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.

This would allow you to remove the enable_testing call in test/CMakeLists.txt, since the CTest submodule calls enable_testing internally.




回答2:


Just to update this.

cmake in version 3.9 added support for GoogleTest integration with CTest.

So you can now get CTest to scrape all of the test macros in your test executable, not just the whole executable.

Example here: https://gist.github.com/johnb003/65982fdc7a1274fdb023b0c68664ebe4



来源:https://stackoverflow.com/questions/13550153/no-tests-found-when-using-gtest-with-cmake-ctest

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