Is it possible to set a TearDownIfFailed from a Test Listener level or a Test Suite level

I am looking to run a test case any time we detect a test failure. From what I can find I would have to set the TearDownIfFailed method for every test case if I want to do this. Is there a way that I can set this once in the test listener or a few times at the test suite level instead of adding it to every test.

For context my goal is to check if there is a crash detected only if a test failed on an iOS device and so that I can send up the crash log and the app logs. I know that I can call a test case in the test listener for the @AfterTestCase to check if we see a crash but this test takes a few seconds to run which significantly adds to the execution time over the course of a few hundred tests where we might only have a few failures.

1 Like

Hey,

I found this sample might be helpful How to use tearDownIfPassed and tearDownIfFailed - #5 by musaffir.puthukudi

1 Like

Okay thanks it looks like it will currently only work on the test case level.

I did find a solution at least at the test listener level and thought I should share it here. I have not worked on trying to get it to work on the test suite level but it would probably work the same. Inside the test listener in the AfterTestCase section:

@AfterTestCase
	def afterTestCase(TestCaseContext testCaseContext) {
		
		if (testCaseContext.getTestCaseStatus() != 'PASSED') {
			'Commands to run if test case has error or failure'
		}
	}

The Test Suite solution is pretty similar:

@TearDownTestCase(skipped = false) // Please change skipped to be false to activate this method.
def tearDownTestCase(TestCaseContext testCaseContext) {
	KeywordUtil.logInfo(testCaseContext.getTestCaseStatus())
	
	if (testCaseContext.getTestCaseStatus() != 'PASSED') {
		'Code to execute when test case fails'
	}
}

1 Like

Awesome, thank you for your contribution. Other people can benefit from your solution. Great work!