How to execute specific test cases from command line interface


#1

I’m not sure if anyone has written about this. There were several questions on executing specific Katalon Studio based on conditions or criteria from external systems. This sample demonstrates a method to achieve this.

What I used:

  • Profiles and global variables
  • The ability to specify profiles and global variables from the command line interface (CLI)
  • Test suites
  • Test listeners

To keep things simple, I had two test cases - My First Test Case and My Second Test Case - that only print out their names upon execution.

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.comment("My First Test Case")
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.comment("My Second Test Case")

Next, I created a test suite containing both test cases. Executing this test suite will executing two test case. To execute a specific test case I needed to skip the other one, which can be achieved using a test listener and the TestCaseContext.skipThisTestCase() method.

In order to allow specifying test cases from CLI, I made use of global variables. I create a global variable named testCaseFilter, and in the default profile, its value is "" (empty string). This variable contains a list of test cases allowed to be executed, separated by commas. For example, it can take the following values:

  • My First Test Case
  • My First Test Case, My Second Test Case
  • Empty (which means executing all test cases)

The test listener (TestCaseFilter.groovy) will read the global variable above and decide which ones to skip.

import org.apache.commons.lang3.StringUtils

import com.kms.katalon.core.annotation.BeforeTestCase
import com.kms.katalon.core.context.TestCaseContext
import com.kms.katalon.core.util.KeywordUtil

import internal.GlobalVariable as GlobalVariable

class TestCaseFilter {
	
	private boolean allowAll;
	
	private String[] allowedTestCaseIds;
	
	public TestCaseFilter() {
		if (StringUtils.isNotBlank(GlobalVariable.testCaseFilter)) {
			allowAll = false;
			allowedTestCaseIds = GlobalVariable.testCaseFilter.split(",").collect({ "Test Cases/${it.trim()}"})
			KeywordUtil.logInfo("Only allow ${allowedTestCaseIds}")
		} else {
			allowAll = true;
			KeywordUtil.logInfo("Allow all test cases")
		}
	}
	
	/**
	 * Executes before every test case starts.
	 * @param testCaseContext related information of the executed test case.
	 */
	@BeforeTestCase
	def sampleBeforeTestCase(TestCaseContext testCaseContext) {
		if (!allowAll) {
			String nextTestCaseId = testCaseContext.getTestCaseId()
			if (!allowedTestCaseIds.contains(nextTestCaseId)) {
				KeywordUtil.logInfo("Skip test case '${nextTestCaseId}'")
				testCaseContext.skipThisTestCase()
			}
		}
	}
}

Now it’s time to experiment. Triggering the execution from the following command

./katalon -noSplash  -runMode=console -projectPath="c:\data\dynamic-execution\dynamic-execution.prj" -retry=0 -testSuitePath="Test Suites/All Test Cases" -executionProfile="default" -browserType="Chrome" -g_testCaseFilter="My Second Test Case"

yields the desired result

2019-01-03 23:57:05.655 INFO  c.k.katalon.core.main.TestSuiteExecutor  - START Test Suites/All Test Cases
2019-01-03 23:57:05.703 INFO  c.k.katalon.core.main.TestSuiteExecutor  - hostName = haimnguyen - 192.168.99.1
2019-01-03 23:57:05.705 INFO  c.k.katalon.core.main.TestSuiteExecutor  - os = Windows 10 64bit
2019-01-03 23:57:05.706 INFO  c.k.katalon.core.main.TestSuiteExecutor  - hostAddress = 192.168.99.1
2019-01-03 23:57:05.707 INFO  c.k.katalon.core.main.TestSuiteExecutor  - katalonVersion = 5.10.1.1
2019-01-03 23:57:05.976 INFO  com.kms.katalon.core.util.KeywordUtil    - Only allow [Test Cases/My Second Test Case]
2019-01-03 23:57:06.070 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-01-03 23:57:06.070 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/My First Test Case
2019-01-03 23:57:06.115 INFO  com.kms.katalon.core.util.KeywordUtil    - Skip test case 'Test Cases/My First Test Case'
2019-01-03 23:57:06.125 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/My First Test Case
2019-01-03 23:57:06.132 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-01-03 23:57:06.132 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/My Second Test Case
2019-01-03 23:57:06.266 DEBUG testcase.My Second Test Case             - 1: comment("My Second Test Case")
2019-01-03 23:57:06.334 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - My Second Test Case
2019-01-03 23:57:06.336 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/My Second Test Case
2019-01-03 23:57:06.396 INFO  c.k.katalon.core.main.TestSuiteExecutor  - --------------------
2019-01-03 23:57:06.396 INFO  c.k.katalon.core.main.TestSuiteExecutor  - END Test Suites/All Test Cases
2019-01-03 23:57:06.396 INFO  c.k.katalon.core.main.TestSuiteExecutor  - ====================

You can modify this POC to query test cases from JIRA or test management systems.


What is the difference between @BeforeTestCase and @SetupTestCase
#2

#3