博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
testng 监听器_TestNG侦听器
阅读量:2532 次
发布时间:2019-05-11

本文共 8219 字,大约阅读时间需要 27 分钟。

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.3Test3 test methodTest3 testString method, input = ATest3 testString method, input = BPASSED: testPASSED: testString("A")PASSED: testString("B")===============================================    Default test    Tests run: 3, Failures: 0, Skips: 0==============================================================================================Default suiteTotal 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.3Test3Listener constructorTestNG suite default output directory = /Users/pankaj/Documents/eclipse-github/TestNG-Examples/test-output/Default suiteContext Name = Default testTest Started. 1527662805150Test3 test methodTest Success. 1527662805159Test Started. 1527662805163Test3 testString method, input = ATest Success. 1527662805164Test Started. 1527662805165Test3 testString method, input = BTest 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: testPASSED: 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 suiteTotal 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侦听器在监视测试套件和测试用例方面非常强大。 其中一些可用于在运行时更改测试行为。

. 检出项目代码。

翻译自:

testng 监听器

转载地址:http://cmqzd.baihongyu.com/

你可能感兴趣的文章
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>