# Perform step(s) if test case fails

**URL:** <https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [May 13, 2019, 7:37am UTC](https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181 "2019-05-13T07:37:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbarash](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@mbarash](https://forum.katalon.com/u/mbarash)\
**Post date:** [May 13, 2019, 7:37am UTC](https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181/1 "2019-05-13T07:37:40Z")

</div>

Hi all,

Is there a way to have Katalon Studio perform specific steps/keywords if the **test case** fails?  
To clarify - I don’t mean failure handling which can be performed on specific steps, what I need is like a catch-all step that will be performed if the test case itself fails.

Thanks!

---

<div class="post-metadata">

**Author:** ![Mariusz](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mariusz/32/386_2.png) [@Mariusz](https://forum.katalon.com/u/Mariusz)\
**Post date:** [May 13, 2019, 8:20am UTC](https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181/2 "2019-05-13T08:20:30Z")

</div>

Maybe that will help you: [How to extract the test status (Pass/Fail) of a test\_case/test\_suite into a variable](http://forum.katalon.com/t/how-to-extract-the-test-status-pass-fail-of-a-test-case-test-suite-into-a-variable/6341/2)  
Combine that with SetUp/TearDown TS methods.

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [May 13, 2019, 8:25am UTC](https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181/3 "2019-05-13T08:25:08Z")

</div>

Hello,

this may be a solution - create another test case and call the original one in try-catch block

```
try {
	WebUI.callTestCase(findTestCase('Misc/FailedTC'), [:], FailureHandling.STOP_ON_FAILURE)
} catch (StepFailedException ex) {
	println "Test case has failed"
    // handle failure here
}
```

---

<div class="post-metadata">

**Author:** ![prabhat.pk589](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@prabhat.pk589](https://forum.katalon.com/u/prabhat.pk589)\
**Post date:** [May 13, 2019, 2:20pm UTC](https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181/4 "2019-05-13T14:20:39Z")

</div>

please use the below code 🙂

```auto
//this is at Test steps level in test case

@TearDownIfFailed

public void Teardown(){
if(testCaseContext.getTestCaseStatus()== 'Failed'){

System.out.println("Test case is failed")
KeywordUtil.markPassed("Test case is failed")
WebUI.closeBrowser
}
}

//Create Listener for test case opr test suite

class TestBase {
	/**
	 * Executes before every test case starts.
	 * @param testCaseContext related information of the executed test case.
	 */
	@BeforeTestCase
	def sampleBeforeTestCase(TestCaseContext testCaseContext) {
		println testCaseContext.getTestCaseId()
		println testCaseContext.getTestCaseVariables()
	}

	/**
	 * Executes after every test case ends.
	 * @param testCaseContext related information of the executed test case.
	 */
	@AfterTestCase
	def sampleAfterTestCase(TestCaseContext testCaseContext) {
		println testCaseContext.getTestCaseId()
		println testCaseContext.getTestCaseStatus()
		
	}

	/**
	 * Executes before every test suite starts.
	 * @param testSuiteContext: related information of the executed test suite.
	 */
	@BeforeTestSuite
	def sampleBeforeTestSuite(TestSuiteContext testSuiteContext) {
		println testSuiteContext.getTestSuiteId()
	}

	/**
	 * Executes after every test suite ends.
	 * @param testSuiteContext: related information of the executed test suite.
	 */
	@AfterTestSuite
	def sampleAfterTestSuite(TestSuiteContext testSuiteContext) {
		println testSuiteContext.getTestSuiteId()
	}
}

```

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [May 13, 2019, 2:34pm UTC](https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181/5 "2019-05-13T14:34:41Z")

</div>

> [@Stop and mark test case as error from listener](http://forum.katalon.com/t/stop-and-mark-test-case-as-error-from-listener/20101/5):
>
> Of course, I should have known that… Your code in the Test Case is a method (in a Groovy Script class). Your “final” throw has to come from there to stop it in its tracks. Sorry, I really missed the point back there. For reference, this is how my TCs look: import ... public class Test extends com.my\_company.some\_page\_object { Test() { // Test steps ... } } try { new Test() passThisTest() } catch(Exception e) { failThisTest(e.message) } So anywhere I perform a throw (or an…

---

<div class="post-metadata">

**Author:** ![mbarash](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@mbarash](https://forum.katalon.com/u/mbarash)\
**Post date:** [May 13, 2019, 3:03pm UTC](https://forum.katalon.com/t/perform-step-s-if-test-case-fails/25181/6 "2019-05-13T15:03:49Z")

</div>

Thanks to everyone for the help!  
TearDownIfFailed is what I needed in my case.
