【问题标题】:How to rerun the failed tests with ctest?如何使用 ctest 重新运行失败的测试?
【发布时间】:2012-11-25 00:37:01
【问题描述】:

我正在使用 CTest 来启动我的项目的测试。我只想启动上次执行失败的测试。

有没有一种简单的方法可以用 CTest 做到这一点?

【问题讨论】:

    标签: unit-testing testing ctest


    【解决方案1】:

    --rerun-failed 选项已添加到 CMake 3.0 中的 CTest:

     --rerun-failed
        Run only the tests that failed previously
    
        This  option  tells  ctest to perform only the tests that failed
        during its previous run.  When this option is  specified,  ctest
        ignores  all  other options intended to modify the list of tests
        to run (-L, -R, -E, -LE, -I, etc).  In the event that CTest runs
        and   no   tests  fail,  subsequent  calls  to  ctest  with  the
        --rerun-failed option will  run  the  set  of  tests  that  most
        recently failed (if any).
    

    参考资料:

    【讨论】:

    • 这是目前最好的答案
    【解决方案2】:

    我认为简短的回答是否定的。

    但是,您可以使用简单的 CMake 脚本将上次失败的测试列表转换为适合 CTest's -I option 的格式。

    CTest 写入一个名为<your build dir>/Testing/Temporary/LastTestsFailed.log 的文件,其中包含失败测试的列表。如果所有测试都通过后续运行,则不会清除此列表。此外,如果 CTest 在仪表板模式下运行(作为 dart 客户端),则日志文件名将包括文件 <your build dir>/Testing/TAG 中详述的时间戳。

    下面的脚本没有考虑包含时间戳的文件名,但应该很容易扩展它来做到这一点。它读取失败的测试列表并将名为FailedTests.log 的文件写入当前构建目录。

    set(FailedFileName FailedTests.log)
    if(EXISTS "Testing/Temporary/LastTestsFailed.log")
      file(STRINGS "Testing/Temporary/LastTestsFailed.log" FailedTests)
      string(REGEX REPLACE "([0-9]+):[^;]*" "\\1" FailedTests "${FailedTests}")
      list(SORT FailedTests)
      list(GET FailedTests 0 FirstTest)
      set(FailedTests "${FirstTest};${FirstTest};;${FailedTests};")
      string(REPLACE ";" "," FailedTests "${FailedTests}")
      file(WRITE ${FailedFileName} ${FailedTests})
    else()
      file(WRITE ${FailedFileName} "")
    endif()
    

    然后您应该能够通过执行以下操作仅运行失败的测试:

    cmake -P <path to this script>
    ctest -I FailedTests.log
    

    【讨论】:

      【解决方案3】:

      基于弗雷泽回答的 Linux 单线:

      ctest -I ,0,,`awk -F: '{print $1;}' Testing/Temporary/LastTestsFailed.log | paste -d, -s`
      

      【讨论】:

        猜你喜欢
        • 2016-04-16
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        • 1970-01-01
        • 2023-02-15
        • 2018-07-26
        相关资源
        最近更新 更多