CMake: How to specify directory where ctest should look for executables?

Viewed 15914

I wanted to integrate ctest to a c++/c project. I use google tests to write unit tests.

Relevant part of my CMakeLists.txt looks like this:

...
####### CREATING EXE #######
add_executable(test_exe main.cpp test.cpp)
target_link_libraries(test_exe GTest::GTest GTest::Main)
set_target_properties (test_exe PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${UNIT_TEST_BIN_OUTPUT_DIR})
add_test(test_exe test_exe)

As you can see i specified the output directory of my executable (UNIT_TEST_BIN_OUTPUT_DIR).

The executable works fine on its own when I use the terminal:

cd <UNIT_TEST_BIN_OUTPUT_DIR>
./test_exe

I want to use ctest to execute my tests. So I go to the "ctest folder" generated by cmake. Here I want to use ctest to execute all test added by "add_test" in cmake.

user@user:~/<dir to cmake>/cmake/unit_tests$ ctest
Test project /<dir to cmake>/cmake/unit_tests
    Start 1: test_exe
Could not find executable test_exe
Looked in the following places:
test_exe
test_exe
Release/test_exe
Release/test_exe
Debug/test_exe
Debug/test_exe
MinSizeRel/test_exe
MinSizeRel/test_exe
RelWithDebInfo/test_exe
RelWithDebInfo/test_exe
Deployment/test_exe
Deployment/test_exe
Development/test_exe
Development/test_exe
Unable to find executable: test_exe
1/1 Test #1: test_exe ......***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - test_exe (Not Run)
Errors while running CTest

If I put the "test_exe" in one of the shown paths it works fine. But I don't want them to be there.

My Question:

Is there a way to tell ctest it should look in UNIT_TEST_BIN_OUTPUT_DIR in order to find the executable?

3 Answers

Using CMake 3.20 and greater, you can tell CTest which directory contains your tests by using a CLI option:

ctest --test-dir /path/to/your/tests

This is a less-invasive solution for existing tests, for which you don't want to modify the CMake files.

Related