[CMake] setup enviroment for all tests
Nils Gladitz
nilsgladitz at gmail.com
Thu Jul 31 03:05:16 EDT 2014
On 07/31/2014 01:21 AM, Leek, Jim wrote:
> I recently converted my project to configure with cmake, and am now
> trying to get the tests to run under ctest. I may be doing this
> completely incorrectly, so let me know if anything you see below looks
> like a bad idea.
>
> All my tests use a particular program to check the output of the tests
> against the correct output. The tests output in a particular format, so
> I have to use a particular 'diff' program to compare them. This program
> was not developed by me, and is not part of the project. So, I wrote a
> FindXXXX.cmake file to find the program and supply the location.
>
> So, I have the path to the diff program as XXXX_BINDIR.
>
> My tests are all driven by a generic script. This script takes a couple
> of command line arguments to get the important paths for running the
> test and checking the results. Like this:
>
> add_test (test1 ${TEST_SCRIPT_DIR}/test.sh
> ${PROJECT_BINARY_DIR}/bin/meos ${CMAKE_CURRENT_SOURCE_DIR} test1.input)
>
> If it's not obvious, test.sh is the test script. The first argument is
> the path to the binary I'm testing, the second is the directory where
> both the input files and expected output files can be found, and the
> last is the name of the input for this test.
>
> So, I could pass the path to the 'diff' program as another command line
> argument, but that's already beginning to become cumbersome. It's seems
> more reasonable just to stick it in the system PATH variable.
>
> I can do that like this:
> set_property(TEST test1 PROPERTY ENVIRONMENT
> PATH="${XXXX_BINDIR}:$ENV{PATH}")
>
> The only annoying thing is, I have to have that line for EVERY test. I
> have lots of tests. I would rather set that path for ALL the tests.
> But I can't seem to find a way to do that. This doesn't work:
> set_property(GLOBAL PROPERTY ENVIRONMENT PATH="${XXXX_BINDIR}:$ENV{PATH}")
>
> Is there a way to set up the environment for all the tests in one line?
What I would do is define a custom function that performs all the tasks
related to setting up one specific class of tests:
function(add_my_test TEST_NAME TEST_INPUT)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_SCRIPT_DIR}/test.sh
${PROJECT_BINARY_DIR}/bin/meos ${CMAKE_CURRENT_SOURCE_DIR} ${TEST_INPUT})
set_property(TEST ${TEST_NAME} PROPERTY ENVIRONMENT
"PATH=${XXXX_BINDIR}:$ENV{PATH}")
endfunction()
add_my_test(test1 test1.input)
add_my_test(test2 test2.input)
...
(untested - written from memory)
Nils
More information about the CMake
mailing list