CMake how to install test files with unit tests

Sure, you can do this on the configuration step this way:

execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${fileFrom} ${fileTo})

If your input files depend on something produced by build, you can create a target for it and add it to the all target:

add_custom_target(copy_my_files ALL
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${fileFrom} ${fileTo}
    DEPENDS ${fileFrom}
)

You may use configure_file with parameter COPYONLY. And perform copying to your build dir: ${CMAKE_CURRENT_BINARY_DIR}


The question is quite old, but in my opinion there is a better solution to the problem than copying the files you need to ${CMAKE_CURRENT_BINARY_DIR}). The add_test command has a WORKING_DIRECTORY option that allows to chose the directory where the tests are run. So I would suggest the following solution:

add_executable(testA testA.cpp)
add_test(NAME ThisIsTestA COMMAND testA WORKING_DIRECTORY ${DIRECTORY_WITH_TEST_DATA})

This avoids needless copying of your input files.


This may not be the best solution, but currently I am doing this:

file(COPY my_directory DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

Which seems to be doing the trick.