如何用ctest重新运行失败的测试?

时间:2012-11-25 00:37:01

标签: unit-testing testing ctest

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

使用CTest有一种简单的方法吗?

3 个答案:

答案 0 :(得分:9)

在CMake 3.0中将--rerun-failed选项添加到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).

参考文献:

答案 1 :(得分:4)

简短的回答是不,我想。

但是,您可以使用简单的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

答案 2 :(得分:0)

基于弗雷泽的答案的Linux one-liner:

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