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

感情迁移 提交于 2019-12-04 04:20:10

问题


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?

回答1:


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

add_test(NAME ${test_name} COMMAND "${test_src}"
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)



回答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.



来源:https://stackoverflow.com/questions/33231290/how-to-tell-a-test-under-cmake-ctest-where-to-find-its-input-data-without-ch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!