Ask a Question

Groovy script to re-run failed tests

Community Hero

Groovy script to re-run failed tests

As @Olga_T suggested here is a script which re-runs some failed tests. The script is put in the TearDown Script section at the project level.


def testSuiteResults = context.getTestRunner().getResults()
def testCaseResults
def runCount = 0
def testPassed = true

// Iterate through each test case in the project
testSuiteResults.each {
	it.getResults().each {
		// PASSED test case
		if ( it.getStatus().toString().equals("PASS") ) { "The test case '" + it.getTestCase().getName() + "' has passed!"
		// FAILED test case
		} else {
			testPassed = false "The test case '" + it.getTestCase().getName() + "' has failed!"
			// Rerunning the failed test case...
			while ((runCount < 2) && (testPassed == false)) { "Rerunning the test one more time..."
// Ugly stuff. Sorry mom! if (it.getTestCase().run(new, false).getStatus().toString().equals("PASS")) { testPassed = true "The test has eventually passed!" } else { "No luck... :(" } runCount++ } } } }

If my answer helped please click on the 'Accept as Solution' button.
SmartBear Alumni (Retired)

Wow, that was a really quick reply Smiley Happy

Well done, @Lucian! Thanks for sharing

Olga Terentieva
SmartBear Assistant Community Manager

Community Hero

Well thanks! Nevertheless in order for this to be really helpful a solution would need to be found for


For now the execution status just stays as FAIL.

If my answer helped please click on the 'Accept as Solution' button.

Hi Lucian,


Your script works fine with my sample project (attached)! I'm not sure I can replicate the situation when "the execution status just stays as FAIL". Am I missing smth?


BTW, I'd correct the script so that it could rerun each TestCase until it passes, but not more than N times.


Clarifications are in the screenshot below:



Customer Care Team

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓

hey, sorry for the delayed response. well... even in your printscreen the TestCase 1 appears as red. So it doesn't matter that it eventually passed - it will be failed in the reports.

This is not very handy. If I would have an automated Jenkins build that build would not be green in the described situation.

I looked for a way to 'forget' the previous failed run of a particular test case but I didn't manage to find a solution...

I'm on the road now. Will give more clarifications later if needed.

If my answer helped please click on the 'Accept as Solution' button.
Occasional Visitor



Did you manage to get a solution to jenkins automated builds? rerunning failing tests would get a green in my jenkins pipeline 🙂


Thanks in advance

Sorry @Pochamen ,


Nothing new under the sun.

If my answer helped please click on the 'Accept as Solution' button.
Showing results for 
Search instead for 
Did you mean: