No tests found when using gtest with cmake/ctest
Asked Answered
G

2

21

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?

Gilbertogilbertson answered 25/11, 2012 at 10:29 Comment(0)
G
42

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.

Gwenora answered 25/11, 2012 at 14:19 Comment(0)
I
3

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

Itis answered 9/1, 2018 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.