cmakectest

How to check if file was created by test in ctest


ctest framework is great framework for testing output and error code. E.g.:

add_test(NAME VersionTest COMMAND myapp.exe --help)
set_tests_properties(VersionTest PROPERTIES PASS_REGULAR_EXPRESSION "Version: 2.2.2")

In my case, an executable apart from output and error code creates also file (e.g. res.json)

I would like ctest also validate if file was created. How I can do that?

I am expecting that if file was not created the test will fail (The ctest --test-dir build command run all tests and the one that check file existence will fail). I do NOT want to validate res.json file contents etc., only its existence.


Solution

  • Here is a solution I came up with. It requires 3.18 though since it uses the new cat functionality though.

    set(json_file ${CMAKE_CURRENT_BINARY_DIR}/res.json)
    
    # Create the json file (In my example I use CMake to create the file)
    add_test(NAME create_file COMMAND ${CMAKE_COMMAND} -E touch ${json_file})
    
    # This test will be run after create_file since we defined the dependency.
    # cmake -E cat <file> will fail if the file doesn't exist.
    add_test(NAME check_file_exists COMMAND ${CMAKE_COMMAND} -E cat ${json_file})
    set_tests_properties(check_file_exists PROPERTIES DEPENDS create_file)
    

    NOTE: You can use other CMake builtin command-line tools like md5sum instead of cat to verify the files existence. However, I'd argue it's a little non-intuitive.

    It's common to cat a file to check it's contents.

    Also using cat sets you up to use other test properties like PASS_REGULAR_EXPRESSION if you wanted to.