2
votes

I have a cmake project that runs through a number of tests, i.e.

add_test(test_title executable arg1 arg2)

On runing these tests a number of files are produced. Once the test has run I would like to delete one of these files produced, i.e.

delete(${arg1}.txt)

    or

delete(${arg2}.pdf)

If you could provide an example it would be very much appreciated.

2
Have you considered adjusting your tests to write to tempfiles? - Torbjörn
Hi Torbjörn, Thanks for replying. It is not the cmake tests that are creating the files but the executable, I would have assumed cmake to have some kind of post-test clean-up methods available. Currently when the tests are run a large amount of data is created, deleting intermediary files after each test would be beneficial. - David Walker
The test executables themselves should be adjusted to write to temporary files when run within a test environment, i.e. the output path for functions under test producing the files should be configurable. That has nothing to do with testing in general, but general design of the library/program. - Torbjörn

2 Answers

1
votes

As advised in the letter, уou can wrap actual testing command with cmake script. In your case it can be:

add_test(NAME test_title
  COMMAND ${CMAKE_COMMAND}
    -Darg1=${arg1}
    -Darg2=${arg2}
    -P ${CMAKE_CURRENT_SOURCE_DIR}/runtest.cmake
)

And the wrapper runtest.cmake can be:

execute_process(COMMAND executable ${arg1} ${arg2}
  TIMEOUT 1000 # it should be less than in add_test
  RESULT_VARIABLE status
)
file(REMOVE ${arg1}.txt)
file(REMOVE ${arg2}.txt)
if(status)
  MESSAGE(FATAL_ERROR "Test executing status: ${status}")
endif()
0
votes

I am not sure whether this option was available at the time of asking the question, but I used a hack along the lines of:

  • define a test: test_title
  • define another test: test_title_remove_file
    • depends on test_title (so test_title will be run first)
    • requires files you want to delete to exist (explicit is better than implicit)
    • calls cmake -E remove <file> doc

This way you'll have one fake extra test but at least it will tell you if there are any errors.

My implementation:

add_test(NAME test_title_remove_file
    COMMAND ${CMAKE_COMMAND} -E remove
        ${arg1}
        ${CMAKE_CURRENT_BINARY_DIR}/${arg2}
)

set_tests_properties(test_title_remove_xmls PROPERTIES
    DEPENDS test_title ## note it is your test_title
    REQUIRED_FILES 
    ${arg1};
    ${CMAKE_CURRENT_BINARY_DIR}/${arg2};
)