Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

googletest - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...