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.
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'
}
}