[CMake] pipe add_test() output into a file

Nils Gladitz nilsgladitz at gmail.com
Tue Aug 26 06:03:37 EDT 2014


On 08/26/2014 11:50 AM, Nico Schlömer wrote:
> I would like to add a test for a file converter. The output of the
> converter is written out to stdout, and I would like to direct this
> into a file to compare it against a reference file. The signature of
> add_test() [1] however doesn't seem to allow that.
>
> How would you go about this problem?

You could run cmake in script mode (${CMAKE_COMMAND} -P) and have the 
script call your converter with execute_process().

You can pass the path to the converter with the $<TARGET_FILE:tgt> 
generator expression and -D (-D options have to be listed before -P).

Note that generator expressions in tests are only supported when the 
test is defined with the NAME/COMMAND signature of add_test().

execute_process() can capture the output of the command in a file or 
variable.

The script could then also perform the comparison.

You can make the script (and the test) fail with e.g. 
message(FATAL_ERROR "message")

Nils


More information about the CMake mailing list