0

我正在使用 CMake 和 ctest 来生成软件测试。例如,我有一个二进制文件foo,它恰好有三个输入参数 p1, p2, p3. 参数范围为 0-2。foo要使用所有可能的组合检查我的二进制文件p1, 我在我的 CMakeList.txt 中执行以下p2操作p3

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)

是否有更“优雅”的方式来生成所有这些不同的测试?假设foo需要 10 个参数p1,...,p10这看起来很可怕。提前致谢。

4

1 回答 1

1

您可以使用递归函数使测试的生成“更优雅”:

#  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.

请注意,在第二种情况下(迭代有 10 个参数),测试的总数相对较大(1536)。在这种情况下,CMake 配置可能会很慢。

通常,这种可扩展的测试是由特殊的测试系统来执行的。CTest(使用 command 生成测试add_test)是一个具有一些功能的简化测试系统。

于 2016-03-03T19:13:42.270 回答