Search code examples
ctestingcmakectest

Cmake ctest generation


i am using CMake together with ctest to generate software tests. As an example i have a binary foo which get's exactly three input arguments p1 , p2, p3. The Parameters can range from 0-2. To check my binary foo with all possible combination of p1, p2, p3 i do the following in my CMakeList.txt

foreach(P1 0 1 2)
    foreach(P2 0 1 2)
        foreach(P3 0 1 2)
            add_test(foo-p1${P1}-p2${P2}-p3${P3} foo ${P1} ${P2} ${P3})
        endforeach(P3)
    endforeach(P2)
 endforeach(P3)

Is there a more "elegant" way to generate all these different tests? Assume foo needs 10 Parameter p1,...,p10 this would look horrible. Thanks in advance.


Solution

  • You may use recursive functions for make test's generation "more elegant":

    #  generate_tests n [...]
    #
    # Generate test for each combination of numbers in given range(s).
    #
    # Names of generated tests are ${test_name}-${i}[-...]
    # Commands for generated test are ${test_command} ${i} [...]
    #
    # Variables `test_name` and `test_command` should be set before function's call.
    function(generate_tests n)
        set(rest_args ${ARGN})
        list(LENGTH rest_args rest_args_len)
        foreach(i RANGE ${n})
            set(test_name "${test_name}-${i}")
            list(APPEND test_command ${i})
            if(rest_args_len EQUAL 0)
                add_test(${test_name} ${test_command}) # Final step
            else()
                generate_tests(${test_args}) # Recursive step
            endif()
        endforeach()
    endfunction()
    
    # Usage example 1
    set(test_name foo)
    set(test_command foo)
    generate_tests(2 2 2) # Will generate same tests as in the question post
    
    # Usage example 2
    set(test_name bar)
    set(test_command bar arg_first ) # `arg_first` will prepend generated command's parameters.
    generate_tests(1 2 1 1 1 1 1 1 1 1) # 10 Ranges for generation. 3 * 2^9 = 1536 tests total.
    

    Note, that in the second case (with 10 parameters for iterate) total number of tests is relatively large(1536). CMake configuration could be slow in that case.

    Normally, such extensible testing is performed by special testing systems. CTest (for which tests are generated with command add_test) is a simplified testing system with a few features.