[CMake] testing with standard output

James C. Sutherland James.Sutherland at utah.edu
Tue Jul 14 10:46:11 EDT 2009


On Jul 14, 2009, at 6:48 AM, Henrique Almeida wrote:

> Hello,
>
> When running "make test", some of my unit tests require that the
> result is given by a correct output, instead of (just) the correct
> return value (because, I'm specifically testing the ability to do
> output). Is there a way to configure CTest to report a successful
> result for certain tests by comparing their outputs to a reference
> output ?
>
> In the most general case I'd need something like:
>
> add_test(test source1 source2 INPUT test.input EXPECTED_OUTPUT
> test.output EXPECTED_ERROR test.error)
>
> I'd provide test.input as stdin, test.output as expected stdout and
> test.error as expected stderr.
>

I asked a similar question recently.  Here is the recipe for comparing  
files in a test.  You will need to adapt this to your own needs...

1. In your CMakeLists.txt file:

#-------------------------------------------------
set( test_cmd ${CMAKE_BINARY_DIR}/util/create_expr )
set( args "--expr-name=test1 --patch-name=PatchT --field-type=FieldT" )
add_test( createExprTest_1
   ${CMAKE_COMMAND}
   -D test_cmd=${test_cmd}
   -D test_args:string=${args}
   -D output_blessed=${CMAKE_SOURCE_DIR}/util/test/test1.h
   -D output_test=${CMAKE_BINARY_DIR}/util/test1.h
   -P ${CMAKE_SOURCE_DIR}/util/run_test.cmake
)
#-------------------------------------------------


2. In the new "run_test.cmake" file:

#-------------------------------------------------
# some argument checking:
# test_cmd is the command to run with all its arguments
if( NOT test_cmd )
   message( FATAL_ERROR "Variable test_cmd not defined" )
endif( NOT test_cmd )

# output_blessed contains the name of the "blessed" output file
if( NOT output_blessed )
   message( FATAL_ERROR "Variable output_blessed not defined" )
endif( NOT output_blessed )

# output_test contains the name of the output file the test_cmd will  
produce
if( NOT output_test )
   message( FATAL_ERROR "Variable output_test not defined" )
endif( NOT output_test )

# convert the space-separated string to a list
separate_arguments( test_args )
message( ${test_args} )

execute_process(
   COMMAND ${test_cmd} ${test_args}
   COMMAND ${CMAKE_COMMAND} -E compare_files ${output_blessed} $ 
{output_test}
   RESULT_VARIABLE test_not_successful
)

if( test_not_successful )
   message( SEND_ERROR "${output_test} does not match $ 
{output_blessed}!" )
endif( test_not_successful )
#-------------------------------------------------



More information about the CMake mailing list