【问题标题】:Cmake ctest generationCmake ctest 生成
【发布时间】:2016-03-03 15:00:49
【问题描述】:

我正在使用 CMake 和 ctest 来生成软件测试。 举个例子,我有一个二进制 foo 它正好是三个输入参数 p1p2p3。参数范围为 0-2。 使用p1p2p3 的所有可能组合检查我的二进制文件foo 我在我的 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)

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

【问题讨论】:

  • 您没有错过在add_test() 命令行中对foo 本身的调用吗?关于您的问题,我不知道add_test() 命令中有任何“参数范围”选项。所以我认为你不能在 CMake 中进一步优化它。
  • 是的,你是对的,我编辑了我的帖子
  • 一个小的优化是在foreach() 中使用RANGE,比如foreach(P1 RANGE 0 2)。这些东西用于迭代测试参数by CMake itself

标签: c testing cmake ctest


【解决方案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(使用命令add_test 生成测试)是一个具有一些功能的简化测试系统。

【讨论】:

  • 您知道其中一些称为“特殊测试系统”吗?
猜你喜欢
  • 1970-01-01
  • 2021-01-31
  • 2016-04-26
  • 2021-11-09
  • 2020-10-13
  • 2012-07-28
  • 2015-07-06
  • 2017-12-10
  • 1970-01-01
相关资源
最近更新 更多