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?
Joachim W
  • 7,290
  • 5
  • 31
  • 59
  • `add_test` command accepts `WORKING_DIRECTORY`option. 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. – Tsyvarev Oct 20 '15 at 09:00
  • 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? – Joachim W Oct 20 '15 at 09:04

2 Answers2

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}
)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • 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. – user1556435 Aug 26 '23 at 12:08
2

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.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • `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. – Joachim W Oct 20 '15 at 08:19
  • `inputfile` was an example for any input file you have. You can pass this as an argument to your test. I extended my answer. – usr1234567 Oct 20 '15 at 08:27
  • 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. – Joachim W Oct 20 '15 at 08:37
  • 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. – usr1234567 Oct 20 '15 at 08:44
  • Copying is allowed in question 1. Concerning symlinks, see question 3. – Joachim W Oct 20 '15 at 08:50
  • Ah ok. Then I'd say giving the path to your test is what I would consider the standard way. – usr1234567 Oct 20 '15 at 09:30