CMake how to install test files with unit tests

I use CMake to build my system and my unit tests.

I also do the build outside the source.

I found using the ADD_TEST () command that you do not need to install a test executable file (it will be launched only when you run make install, which is great).

However, my unit tests depend on some input files that need to be copied to where the executable is running.

As far as I know, I can not use INSTALL () to copy files there, because I did not indicate where it is - it depends on where the build command is called.

Is there any way that I can tell CMake to copy my test files to the same place where it creates the executable?

+5
source share
4 answers

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

file(COPY my_directory DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

Which seems to do the trick.

+1
source

You can use configure_file with the COPYONLY parameter . And copy to the assembly file: $ {CMAKE_CURRENT_BINARY_DIR}

+7
source

, :

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

, , all:

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

, , , , , , ${CMAKE_CURRENT_BINARY_DIR}). add_test WORKING_DIRECTORY, , . :

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

This avoids unnecessary copying of your input files.

0
source

All Articles