Test Fixtures and Test Listeners (Test Hooks) in Katalon Studio
To extend your current testing flow, the test fixtures (@setUp/@tearDown methods) and test listeners might be useful for prerequisite and clean-up configurations for all the tests.
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.
The infographic below visualizes how Katalon Studio executes your automated test projects with the test fixtures and test listeners methods.
Test listeners are always triggered first if you define both test listeners and activate setUp/tearDown methods simultaneously.
This document guides you through how to configure the setUp/tearDown methods and the test listeners in Katalon Studio.
setUp() and tearDown() for test suite and test case
In Katalon Studio, you can specify prerequisite and clean-up configurations for your test cases using the @setUp and @tearDown methods.
Here are some common usages of the @setUp and @tearDown methods:
Method | Common usage |
---|---|
@setUp |
|
@tearDown |
|
The table below describes the trigger conditions of these methods:
Method | Description | Trigger Condition |
---|---|---|
@setUp | Setup test suite environment. | Before executed test suites. |
@setUpTestCase | Run before each test case starts. | Before executed test cases. |
@tearDown | Clean test suites environment. | After executed test suites. |
@tearDownTestCase | Run after each test case ends. | After executed test cases. |
Activate setUp and tearDown in a test suite
In the test suite editor, switch to the Script tab to view the setUp and tearDown methods template.
By default, the setUp and tearDown methods are not triggered even if they match with provided trigger condition as shown in the below code sample:
/**
* Setup test suite environment.
*/
@SetUp(skipped = true) // Please change skipped to be false to activate this method.
def setUp() {
// Put your code here.
}
To activate the @setUp and @tearDown methods, you need to set the skipped value to false. For example:
@SetUp(skipped = false)
Executing with the setUp/tearDown methods still generates execution logs as usual.
You cannot see setUp and tearDown executed reports from generated Test Suite report. You can only see setUpTestCase and tearDownTestCase in generated Test Suite report.
Add setUp and tearDown in manual view of a test case
In the manual view of a test case, you can add the setUp and tearDown method by following these steps:
Add setUp and tearDown in the script view of a test case
In the script view of a test case, you can declare a method as setUp/tearDown methods using the matching annotation above them. For example:
@com.kms.katalon.core.annotation.SetUp
def setup(def param1, def param2) {
}
@com.kms.katalon.core.annotation.TearDownIfFailed
def teardownIfFailed(def param1, def param2) {
}
@com.kms.katalon.core.annotation.TearDownIfPassed
def teardownIfPassed(def param1, def param2) {
}
@com.kms.katalon.core.annotation.TearDownIfError
def teardownIfError(def param1, def para2) {
}
@com.kms.katalon.core.annotation.TearDown
def teardown(def param1, def param2) {
}
Test Listeners (Test Hooks)
Test listeners are test steps created to "listen" to the event defined in the script and behave accordingly. In this section, you will learn how to create a test listener.
Create new Test Listeners
Test listeners are test artifacts. You can perform all basic operations such as create, copy/cut, rename, or delete with test listeners. To create a new test listener, do as follows:
Example: Using test listeners
In this example, we define multiple environments as different global variables by using test listeners. For more information, see Global variables.
Before you execute your test case, change the environment variable to your preferred environment by following the script below:
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.annotation.BeforeTestCase
import com.kms.katalon.core.annotation.BeforeTestSuite
import com.kms.katalon.core.annotation.AfterTestCase
import com.kms.katalon.core.annotation.AfterTestSuite
import com.kms.katalon.core.context.TestCaseContext
import com.kms.katalon.core.context.TestSuiteContext
/**
* 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'
}
}
To learn more about test listeners, refer to chapter 2 of our Katalon Academy course: Test Listener.