目录

Katalon学习五十九测试监听器Test-Listeners

目录

【Katalon学习五十九】测试监听器Test Listeners

测试监听器 Test Listeners 是Katalon Studio从5.2版本引入的一个新特性。这是一种帮助您扩展当前测试流的伟大而灵活的方法。简单地说,测试侦听器是根据您自己的标准创建的测试步骤,当条件匹配时将执行这些步骤。下面的指南包含了所有有用的信息,可以帮助您开始使用测试侦听器。

管理测试监听器

测试侦听器可以像其他测试工件一样处理,这意味着您可以执行所有基本操作,如创建、复制/剪切、重命名或删除。除了创建一个操作之外,我们不会过多地讨论这些操作。测试监听器位于Test Explorer窗格中。要创建新的测试监听器:

在“测试资源管理器”中右键单击“测试侦听器”。选择新的>新测试监听器。

https://i-blog.csdnimg.cn/blog_migrate/38412e639b18ea4a0f40fde0be89669a.png

在创建一个新的测试监听器时,您可以看到在new test listener对话框中有4个选项:

https://i-blog.csdnimg.cn/blog_migrate/1eaadf4c5db179b4913373c101f6963e.png

Generate sample Before Test Case method在每个测试用例开始之前,将生成一个示例侦听器。
Generate sample After Test Case method在每个测试用例结束之后,将生成一个示例侦听器。
Generate sample Before Test Suite method在每个测试套件开始之前,将生成一个示例侦听器。
Generate sample After Test Suite method在每个测试套件结束后,将生成一个示例listenere。

您可以选择一个或多个选项。一旦完成,Katalon Studio将相应地生成一个示例模板:

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()
	}
}

从上面的代码中可以看到,一个示例生成的模板已经添加了必要的注释、库和受支持的函数,以帮助您将当前的测试流扩展到更高的级别。

  • 测试侦听器没有限制。用户可以创建任意多的首选项。
  • 如果您有多个测试侦听器类,则类本身将在Katalon存储中按字母顺序实例化,只有这样,各个侦听器方法才会自上而下地执行。
  • 测试列表中任何步骤的执行状态都不会影响已执行测试用例的整体状态(e。g:如果您的任何一个测试监听器中有一个失败的输出,但是执行的测试用例的最终状态被传递,那么测试用例的状态将被传递)。

可视化工作流

为了不与setUp和tearDown混淆,下面的可视化工作流演示了Katalon Studio将如何在使用/不使用setUp和tearDown方法的情况下执行测试自动化项目。

https://i-blog.csdnimg.cn/blog_migrate/1856e9b3114fd66162b998dadcdebe35.png

例如

使用测试监听器将多个环境定义为不同的全局变量。在测试用例执行之前,只需将环境变量更改为首选环境。

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