Test Fixtures and Test Listeners (Test Hooks)

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

Automation testers usually want to specify prerequisite and clean-up configurations for their test cases. With the prerequisite configuration, the test engine must take specific actions before starting test execution. For clean-up configuration, the test engine must carry out some actions after the test execution finishes.

Every test suite from your projects now has been equipped 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 useful extension besides Test Listeners to extend your current testing flow as much as possible.

In Test Suite interface, the Script tab generates sample SetUp and TearDown methods to be used.

script tab interface

To not get confused with setUp and tearDown, the following workflow can be used to better visualize how Katalon Studio execute test automation projects with/ without setUp and tearDown methods.

test execution lifecycle

Supported methods

MethodDescriptionTrigger ConditionCommon Usages
setUpSetup test suite environmentBefore executed test suites
  • Prepare a 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

Enable SetUp and TearDown

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

Methods consideration

  • Execution progress from these methods still has 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. You can only see setUpTestCase and tearDownTestCase in generated Test Suite report.
  • Test Listeners will always be triggered first if you define both Test Listeners and activate Setup/ Teardown methods simultaneously.
sample before test suite

In Manual view

MethodDescriptionCommon Usage
Set UpThis method is always called first prior to executing the 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 the test case steps and one of those steps has Error status.
Tear DownThis method will be called finally.

The SetUp()/ TearDown() methods will have Error status if any issue occurs 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:

define method

Test Listeners (Test Hooks)

Test Listeners are test steps created based on your own criterion. Therefore, it's a useful and flexible way to help you extend your current testing flows. When matching the condition, you can execute Test Listeners. To start with Test Listeners, follow the guide below.

Create new Test Listeners

Test Listeners are the same as other test artifacts. You can perform all basic operations such as create, copy/cut, rename or delete with Test Listeners. This section focus on how to create new Test Listeners. In the Tests Explorer pane, you can find Test Listeners location. To create new Test Listener:

  1. In Tests Explorer, right-click on Test Listeners. Select New > New Test Listener.
create new test listener
  1. The New Test Listener dialog appears with 4 options as below:
Test Listeners options
Option Description
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 listener will be generated after every test suite ends.

You can select one or multiple options, then click OK. Once finished, Katalon Studio generates 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 alphabetical 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 the final status of the executed test case is PASSED, then the test case's status will be PASSED).

Example

Define multiple environments as different Global Variables by using Test Listeners. See Global Variables and Execution Profile for more infomation.

Before test case execution, change the environment variable to the preferred environment by following the example below:

/**
 * 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