testng 监听器_TestNG侦听器

testng 监听器

TestNG Listeners are used to inspect and modify the testing behavior. TestNG listeners always extend org.testng.ITestNGListener marker interface. TestNG listeners can be defined for a test class using org.testng.annotations.Listeners annotation.

TestNG侦听器用于检查和修改测试行为。 TestNG侦听器始终扩展org.testng.ITestNGListener标记器接口。 可以使用org.testng.annotations.Listeners注释为测试类定义TestNG侦听器。

TestNG侦听器 (TestNG Listeners)

There are many listeners interfaces provided by TestNG. They all extend org.testng.ITestNGListener interface.

TestNG提供了许多侦听器接口。 它们都扩展了org.testng.ITestNGListener接口。

Let’s look at some of the important TestNG listeners.

让我们看一些重要的TestNG侦听器。

  1. ISuiteListener: We can use this test suite listener to perform some operations when test suite starts and when all the tests are executed. This interface contains two methods – onStart(ISuite suite) and onFinish(ISuite suite) and provides access to test suite object.

    ISuiteListener :当测试套件启动并且执行所有测试时,我们可以使用此测试套件侦听器执行一些操作。 此接口包含两种方法– onStart(ISuite suite)onFinish(ISuite suite)并提供对测试套件对象的访问。
  2. ITestListener: We can use this listener to analyze test methods, perform logging. We can also use them to send notifications if any test fails by implementing onTestFailure(ITestResult result) method.

    ITestListener :我们可以使用此侦听器来分析测试方法,执行日志记录。 通过实现onTestFailure(ITestResult result)方法,如果任何测试失败,我们还可以使用它们发送通知。
  3. IAnnotationTransformer: We can implement this interface to modify the annotations for any @Test method. Note that we can use this annotation only with TestNG XML configuration.

    IAnnotationTransformer :我们可以实现此接口来修改任何@Test方法的注释。 请注意,我们只能将此注释与TestNG XML配置一起使用。
  4. IAnnotationTransformer2: We can implement this interface to modify the annotations for any method other than @Test method.This annotation can be used with TestNG XML configuration only.

    IAnnotationTransformer2 :我们可以实现此接口来修改@Test方法以外的任何方法的注释。该注释只能与TestNG XML配置一起使用。
  5. IConfigurable: If a test class implements this interface, its run() method will be invoked instead of each configuration method found.

    IConfigurable :如果测试类实现此接口,则将调用其run()方法,而不是找到的每个配置方法。
  6. IConfigurationListener: Listener interface for events related to configuration methods.

    IConfigurationListener :与配置方法有关的事件的侦听器接口。
  7. IExecutionListener: This listener is used to monitor when a TestNG run starts and ends.

    IExecutionListener :此侦听器用于监视TestNG运行的开始和结束时间。
  8. IHookable: If a test class implements this interface, its run() method will be invoked instead of each @Test method found.

    IHookable :如果测试类实现此接口,则将调用其run()方法,而不是找到的每个@Test方法。
  9. IInvokedMethodListener: A listener that gets invoked before and after a method is invoked by TestNG.

    IInvokedMethodListener :在TestNG调用方法之前和之后调用的侦听器。
  10. IMethodInterceptor: This class is used to alter the list of test methods that TestNG is about to run.

    IMethodInterceptor :此类用于更改TestNG即将运行的测试方法的列表。
  11. IReporter: This interface can be implemented by clients to generate a report.

    IReporter :客户端可以实现此接口以生成报告。

TestNG侦听器示例 (TestNG Listener Example)

Let’s create a simple TestNG test class, then we will implement few listeners and add it to the class.

让我们创建一个简单的TestNG测试类,然后我们将实现一些侦听器并将其添加到该类中。

package com.journaldev.listeners;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

public class Test3 {

	@Test
	public void test() {
		System.out.println("Test3 test method");
	}
	
	@Test(dataProvider = "dp")
	public void testString(String s) {
		System.out.println("Test3 testString method, input = "+s);
	}
	
	@DataProvider
	public Object[] dp() {
		return new Object[] {"A", "B"};
	}
	
}

When we run above as TestNG test, we get the following output in the console.

当我们在上面作为TestNG测试运行时,我们在控制台中获得以下输出。

[RemoteTestNG] detected TestNG version 6.14.3
Test3 test method
Test3 testString method, input = A
Test3 testString method, input = B
PASSED: test
PASSED: testString("A")
PASSED: testString("B")

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

ISuiteListener示例 (ISuiteListener Example)

Here is a simple implementation of ISuiteListener interface.

这是ISuiteListener接口的简单实现。

package com.journaldev.listeners;

import org.testng.ISuite;
import org.testng.ISuiteListener;

public class Test3SuiteListener implements ISuiteListener {

	@Override
	public void onStart(ISuite suite) {
		System.out.println("TestNG suite default output directory = "+suite.getOutputDirectory());
		}

	@Override
	public void onFinish(ISuite suite) {
		System.out.println("TestNG invoked methods = " +suite.getAllInvokedMethods());
	}

}

ITestListener示例 (ITestListener Example)

Here is a simple implementation of ITestListener interface.

这是ITestListener接口的简单实现。

package com.journaldev.listeners;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class Test3TestListener implements ITestListener{

	public Test3TestListener() {
		System.out.println("Test3Listener constructor");
	}

	@Override
	public void onTestStart(ITestResult result) {
		System.out.println("Test Started. "+result.getStartMillis());
	}

	@Override
	public void onTestSuccess(ITestResult result) {
		System.out.println("Test Success. "+result.getEndMillis());
	}

	@Override
	public void onTestFailure(ITestResult result) {
		System.out.println("Test Failed. "+result.getTestName());
	}

	@Override
	public void onTestSkipped(ITestResult result) {
		System.out.println("Test Skipped. "+result.getTestName());
	}

	@Override
	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {		
	}

	@Override
	public void onStart(ITestContext context) {
		System.out.println("Context Name = "+context.getName());
	}

	@Override
	public void onFinish(ITestContext context) {
		System.out.println(context.getPassedTests());
	}
}

Now modify the Test3 class by adding Listeners to its class definition.

现在,通过在其类定义中添加侦听器来修改Test3类。

@Listeners({Test3TestListener.class, Test3SuiteListener.class})
public class Test3 {
}

When we run the class again as TestNG test, we get following output.

当我们再次运行该类作为TestNG测试时,我们得到以下输出。

[RemoteTestNG] detected TestNG version 6.14.3
Test3Listener constructor
TestNG suite default output directory = /Users/pankaj/Documents/eclipse-github/TestNG-Examples/test-output/Default suite
Context Name = Default test
Test Started. 1527662805150
Test3 test method
Test Success. 1527662805159
Test Started. 1527662805163
Test3 testString method, input = A
Test Success. 1527662805164
Test Started. 1527662805165
Test3 testString method, input = B
Test Success. 1527662805166
[ResultMap map={[TestResult name=test status=SUCCESS method=Test3.test()[pri:0, instance:com.journaldev.listeners.Test3@291caca8] output={null}]=Test3.test()[pri:0, instance:com.journaldev.listeners.Test3@291caca8], [TestResult name=testString status=SUCCESS method=Test3.testString(java.lang.String)[pri:0, instance:com.journaldev.listeners.Test3@291caca8] output={null}]=Test3.testString(java.lang.String)[pri:0, instance:com.journaldev.listeners.Test3@291caca8], [TestResult name=testString status=SUCCESS method=Test3.testString(java.lang.String)[pri:0, instance:com.journaldev.listeners.Test3@291caca8] output={null}]=Test3.testString(java.lang.String)[pri:0, instance:com.journaldev.listeners.Test3@291caca8]}]
PASSED: test
PASSED: testString("A")
PASSED: testString("B")

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

TestNG invoked methods = [Test3.test()[pri:0, instance:com.journaldev.listeners.Test3@291caca8] 689745064, Test3.testString(java.lang.String)[pri:0, instance:com.journaldev.listeners.Test3@291caca8]A  689745064, Test3.testString(java.lang.String)[pri:0, instance:com.journaldev.listeners.Test3@291caca8]B  689745064]

===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

摘要 (Summary)

TestNG listeners are very powerful in monitoring the test suite and test cases. Some of them can be used to alter the testing behavior at runtime.

TestNG侦听器在监视测试套件和测试用例方面非常强大。 其中一些可用于在运行时更改测试行为。

GitHub Repository. GitHub Repository中检出项目代码。

翻译自: https://www.journaldev.com/21252/testng-listeners

testng 监听器

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值