Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 806
rozzy Avatar asked Nov 25 '12 10:11

rozzy


People also ask

What is CTest in CMake?

Description. The ctest executable is the CMake test driver program. CMake-generated build trees created for projects that use the enable_testing() and add_test() commands have testing support. This program will run the tests and report results.


2 Answers

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.

like image 188
Fraser Avatar answered Sep 25 '22 21:09

Fraser


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

like image 27
johnb003 Avatar answered Sep 25 '22 21:09

johnb003