Google Plus Logo
Twitter Logo
Facebook Logo

CMake Testing - Rerun the last failed tests with CTest

Some time ago, we saw how to use CMake to run Boost Tests in paralel, now it is time for another tip.

A feature that I think is lacking in CMake/CTest is a way to launch only the last failed tests. As it is not possible to do that directly, I posted the question on StackOverflow and got a great answer from Fraser. I wanted to share its answer.

CTest has -I option to select a list of tests to run. The idea here is to convert the log of CTest in format readable by CTest. What I think is great in its answer is that the solution is a CMake script:

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})
  file(WRITE ${FailedFileName} "")

This test just transforms one file into another.

You can then run the last failing tests using:

cmake -P 
ctest -I FailedTests.log

Very easy, isn't it ?

There is a limitation to this solution. It won't work when CTest is running in dashboard mode, but it wouldn't take too long to adapt it for that.

Hope you found that tip useful.


Comments powered by Disqus