How to tell a test under cmake/ctest where to find its input data ... without changing hard-coded file names
Asked Answered
B

2

7

Directory prj/test contains some test scripts t01.exe, t02.exe etc. Some of them need input data d01.dat etc, also provided in prj/test. The names of these data files are hard-coded in the tests, and I cannot easily change this. The control file CMakeLists.txt contains

enable_testing()
file(GLOB test_sources "t*")
foreach(test_src ${test_sources})
    string(REGEX REPLACE ".*/" "" test_name "${test_src}")
    string(REGEX REPLACE ".exe$" "" test_name "${test_name}")
    add_test(${test_name} "${test_src}")
endforeach(test_src)

I'm building the project in a subdirectory prj/build. ctest works fine ... until a test requires input data. Obviously, they are not found because they reside in prj/test whereas the test runs in prj/build/test.

Hence my questions:

  • What's the standard way to let the tests find their input data?
  • Is there a way that does not require copying the data (in case they are huge)?
  • True that symlinks don't work under Windows, and therefore are no acceptable solution?
Blazer answered 20/10, 2015 at 8:8 Comment(2)
add_test command accepts WORKING_DIRECTORYoption. You can set this option to prj/test (${CMAKE_SOURCE_DIR}/test) directory, so test will be run from directory where it is located. As I understand from your question, such way test will find data files.Sequela
Great, many thanks! add_test(NAME ${test_name} COMMAND "${test_src}" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) works perfectly. Will you repost your comment as an answer so that I can accept it?Blazer
S
14

add_test command accepts WORKING_DIRECTORY option. You can set this option to a directory where a test is located. So, the test will find its data file:

add_test(NAME ${test_name} COMMAND "${test_src}"
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
Sequela answered 20/10, 2015 at 9:7 Comment(1)
Very helpful! Note that this doesn't set the working directory of the executable when used in Visual Studio. You might want to add set_target_properties(${test_name} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) when running the executables individually from your IDE.Pastypat
G
3

The variables ${CMAKE_CURRENT_SOURCE_DIR} and ${CMAKE_SOURCE_DIR} are helpful. The first one is the source directory to the current binary location. The latter is the root to the top level of the source tree.

Supposed you have an input file at prj/test/inputfile you could get the path to it with${CMAKE_CURRENT_SOURCE_DIR}/inputfile. You can pass the path as an argument to your test.

Gripe answered 20/10, 2015 at 8:12 Comment(6)
inputfile nowhere appears in my CMakeLists.txt, and I would hesitate to make it appear since that might require extra work for any test added later.Blazer
inputfile was an example for any input file you have. You can pass this as an argument to your test. I extended my answer.Gripe
My tests are in a domain-specific language that does not easily admit this solution. I should have said it more explicitely, and now edited the question: Input file names are hard-coded in the tests, and I would rather not change this. I really need a solution at cmake or wrapper shell script level.Blazer
Ok, you don't want to copy the data nor do you want to symlink to them. Additionally, a path to the right location does not work for you. Then there is no other way. Don't use out-of-source builds? A poor advice.Gripe
Copying is allowed in question 1. Concerning symlinks, see question 3.Blazer
Ah ok. Then I'd say giving the path to your test is what I would consider the standard way.Gripe

© 2022 - 2024 — McMap. All rights reserved.