[CMake] compare two files - testing

James C. Sutherland James.Sutherland at utah.edu
Mon Jul 6 10:27:25 EDT 2009


Thank you - this was a very useful explanation!

For any others who may look at this, note that
	execute_command
should be
	execute_process


On Jul 4, 2009, at 1:11 AM, Michael Wild wrote:

>
> On 4. Jul, 2009, at 0:56, James C. Sutherland wrote:
>
>> I have a test that produces output files that I would like to  
>> compare against a "blessed" copy.  Is there a way to do this in  
>> CMake/CTest?  This is probably a very simple thing, but I have not  
>> figured out how to do it.
>>
>> I have an
>> 	add_test( ... )
>> that creates the test, but I am not sure how to add the diff on the  
>> output.
>>
>> FYI, I am running this via "make test" - I am not yet running ctest  
>> directly.
>>
>
>
> cmake -E compare_files file1 file2
> cmake -E md5sum file1 ...
>
> where probably the former is the more appropriate for you (unless  
> you store a "database" of md5 sums of your "blessed" files, so you  
> don't have to store/redistribute them in case they are big). E.g put  
> the following code in run_test.cmake:
>
> # 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" )
> if( 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" )
> if( 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" )
> if( NOT output_test )
>
> execute_command(
>  COMMAND ${test_cmd}
>  COMMAND ${CMAKE_COMMAND} -E compare_files ${output_blessed} $ 
> {output_test}
>  RESULT_VARIABLE test_not_successful
>  OUTPUT_QUIET
>  ERROR_QUIET
>  )
>
> if( test_not_successful )
>  message( SEND_ERROR "${output_test} does not match $ 
> {output_blessed}!" )
> endif( test_not_successful )
>
>
>
> and then in your CMakeLists.txt:
>
> add_test( "someImportantTest"
>  ${CMAKE_COMMAND}
>  -Dtest_cmd="${CMAKE_BINARY_DIR}/tests/someImportantTestProgram -- 
> with arguments"
>  -Doutput_blessed="${CMAKE_SOURCE_DIR}/tests/output/ 
> someImportatanTestProgram.output"
>  -Doutput_test="${CMAKE_BINARY_DIR}/someImportatanTestProgram.output"
>  -P ${CMAKE_SOURCE_DIR}/CMake/run_test.cmake
>  )
>
>
> If your test program writes to STDOUT you can use the OUTPUT_FILE  
> option in the execute_command of your run_test.cmake
>
> HTH
>
> Michael



More information about the CMake mailing list