Test Fixtures and Test Listeners (Test Hooks)

SetUp() and TearDown() for Test Suite and Test Case

Available in Katalon Studio v5.3

For full lifecycle of Tests in Katalon Studio, refer to this documentation.

Automation testers usually want to specify prerequisite and clean-up configuration for their test cases. With the prerequisite configuration, certain actions must be taken before starting test execution. For clean-up configuration, some actions must be carried out after the test execution finishes.

Every test suite from your projects now has been equipped with the ability to run either SetUp or Teardown methods, which are groups of your own defined test steps before or after executing a Test Suite. This feature is another great extension besides Test Listener to extend your current testing flow as much as possible.

There will a new tab called 'Script' in Test Suite's interface. This interface will generate sample Setup and TearDown methods to be used.

Supported methods

MethodDescriptionTrigger ConditionCommon Usages
setUpSetup test suite environment

Before executed test suites

Prepare testing environment

Call required test cases for the executed test suite

setUpTestCaseRun before each test case startsBefore executed test cases
tearDownClean test suites environmentAfter executed test suites

Clean-up testing environment

Call TearDown test cases for the executed test suite

 

 

tearDownTestCaseRun after each test case endsAfter executed test cases

How it works

By default, the Setup and Teardown methods are not triggered even if they match with provided trigger condition above. You need to set skipped value from true to false to activate related methods.

Methods consideration

  • Execution progress from these methods still have execution logs as usual and they will be stored in execution logs files of Katalon Studio.
  • You can't see setUp and teardown executed reports from generated Test Suite report. Only setUpTestCase and tearDownTestCase can be seen in generated Test Suite report
  • Test Listeners will always be triggered first if you define both Test Listeners and activate Setup / Teardown methods at the same time.

In Manual view

MethodDescriptionCommon Usage
Set UpThis method is always called first prior to executing main test steps.

Prepare testing environment such as:

  • Starting new browser with clean cookies
  • Creating temporary & proxy databases, directories
  • Starting a server process
  • ...
Tear Down If FailedThis method will be called after executing all steps of the test case and one of those steps has Failed status.

Clean-up testing environment such as:

  • Closing browsers
  • Disclosing opened connections to database or server
  • ...

 

Tear Down If PassedThis method will be called after executing all steps of the test case and all of those steps have Pass status.
Tear Down If ErrorThis method will be called after executing all steps of the test case and one of those steps has Error status.
Tear DownThis method will be called finally.

The SetUp()/TearDown() methods will have Error status if there is any issue occurred during their execution. The only exception to this is when AssertionError Class is used or the methods are skipped.

In Script view

You can declare a method as setup() or teardown() method using the appropriated annotation above it:

@com.kms.katalon.core.annotation.SetUp
@com.kms.katalon.core.annotation.TearDown
@com.kms.katalon.core.annotation.TearDownIfFailed
@com.kms.katalon.core.annotation.TearDownIfPassed
@com.kms.katalon.core.annotation.TearDownIfError

For example:

Test Listeners (Test Hooks)

Available in Katalon Studio v5.2

Test Listeners is a new feature of Katalon Studio introduced since version 5.2. It is a great and flexible way to help you extend your current testing flows. In simple term, Test Listeners are test steps that created based on your own criterias and will be executed when the condition is matched. The following guide contains all useful information to get you started with Test Listeners.

Manage Test Listeners

Test Listeners can be treated the same as other test artifacts, which means you can perform all basic operations such as create, copy/cut, rename or delete. We will not talk much about these actions except Create one. Test Listeners locate in Test Explorer pane. To create New Test Listener:

Right-click on Test Listeners in Tests Explorer. Select New > New Test Listener.

When creating a new test listener, you can see there are 4 options in New Test Listener dialog:

Generate sample Before Test Case method A sample listener will be generated before every test case starts.
Generate sample After Test Case method A sample listener will be generated after every test case ends.
Generate sample Before Test Suite method A sample listener will be generated before every test suite starts.
Generate sample After Test Suite method A sample listenerwill be generated after every test suite ends.

You can select one or multiple options. Once finished, Katalon Studio will generate a sample template accordingly:

Expand source

class NewTestListener {
	/**
	 * 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()
	}
}

As you can see from the code above, a sample generated template has already added necessary annotations, libraries and supported functions to help you extend your current testing flows to a higher level. 

  • There is no limit on Test Listeners. Users can create as many as preferred.
  • If you have more than one Test Listener class, the classes themselves are instantiated in Katalon storage in alphabetically order, only then are the individual listener methods executed top-down.
  • Execution status of any steps within Test Listers will NOT affect the overall status of the executed test case (e.g: if you have a FAILED output in any of your Test Listeners but final status of the executed test case is PASSED, then test case's status will be PASSED).

Visualized Workflow

To not get confused with setUp and tearDown, the visualized workflows below demonstrate how Katalon Studio will execute test automation project with/without setUp and tearDown methods.

Example

Define multiple environments as different Global Variables by using Test Listeners. Simply change the environment variable to the preferred environment before test case execution.

/**
 * Executes before every test case starts.
 * @param testCaseContext related information of the executed test case.
 */

@BeforeTestCase
def sampleBeforeTestCase(TestCaseContext testCaseContext) {
    if (GlobalVariable.gl_Environment == 'Local') {
        GlobalVariable.gl_Url = 'localhost'
    } else if (GlobalVariable.gl_Environment == 'Staging') {
        GlobalVariable.gl_Url = 'staging'
    }
}

Feedback