Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake how to install test files with unit tests

I am using CMake to build my system and my unit tests.

I am also doing an out-of-source build.

I've found with the ADD_TEST() command, that you don't need to install the test executable (it will just be run when you run make install, which is great).

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

As far as I am aware, I can't use INSTALL() to copy the files there, because I haven't specified where there is - it depends on where the build command is called.

Is there some way I can tell CMake to copy my test files to the same location that it builds the executable?

like image 836
Alex Avatar asked Sep 07 '12 06:09

Alex


People also ask

How do I add a test to CMake?

To add testing to a CMake-based project, simply include(CTest) and use the add_test command.

What does CMake add_test do?

Options To add_test(...) COMMAND specifies the command to run for the test. Essentially, this can be any command that would normally run in your terminal. You can also pass the name of an executable target, and CMake will replace it with the full location of the executable.

Is CTest part of CMake?

CTest is the part of CMake that handles testing your code. CTest allows for an easy way to run your newly built programs with various argument and option settings, and then check the results against expected output.


1 Answers

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.

like image 94
magnetometer Avatar answered Sep 28 '22 01:09

magnetometer