0

I have a project layout like this:

.
|_ CMakeLists.txt
|_ src
| |_ CMakeLists.txt
| |_ myLib
| |_ tests
|   |_ CMakeLists.txt
|   |_ add_test(myLibTest my_lib_test)

The problem is that if I enable testing in src/CMakelists.txt like this:

enable_testing()
add_subdirectory(tests)

CMake compiles my tests, but won't run them:

$ ctest
********************************* 
No test configuration file found!
********************************* 
Usage

  ctest [options]

Why is this?

If I change the layout like this:

.
|_ CMakeLists.txt
|_ src
| |_ CMakeLists.txt
| |_ myLib
|_ tests
| |_ CMakeLists.txt
| |_ add_test(myLibTest my_lib_test)

..and enable testing in ./CMakeLists.txt then I can run the tests, but of course cannot refer to variables defined in src/CMakeLists.txt.

juzzlin
  • 45,029
  • 5
  • 38
  • 50
  • 2
    "Why is this?" - This is by design. If you want to run `ctest` from the the top-level directory, you need to call `enable_testing()` from the top-level directory. – Tsyvarev Mar 06 '19 at 13:34
  • Could you turn that into an answer? :) – juzzlin Mar 06 '19 at 14:02
  • I am not sure what should be answered. You have found by yourself, that moving `enable_testing()` to the top-level directory works. The same is stated in that SO question: https://stackoverflow.com/questions/13550153/no-tests-found-when-using-gtest-with-cmake-ctest/13551858. If question "Why is this?" means "why is it implemented so one need to call `enable_testing()` from the top-level?", then one can only guess (unless he is a CMake developer). Dependent on the actual meaning, your question is either a duplicate or "primary opinion-based". – Tsyvarev Mar 06 '19 at 14:10
  • 1
    I didn't understand the actual root cause of this behavior or whether it should work otherwise. Thus your first comment perfectly answers the question. You are taking this way too literally. – juzzlin Mar 06 '19 at 14:15

0 Answers0