码迷,mamicode.com
首页 > 其他好文 > 详细

TestNG Listener

时间:2016-11-02 07:47:20      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:conf   zed   pre   修改   round   org   ges   package   配置   

  • 常用接口

  • IExecutionListener   监听TestNG运行的启动和停止。

  • IAnnotationTransformer 注解转换器,用于TestNG测试类中的注解。

  • ISuiteListener 测试套件监听器,监听测试套件的启动和停止。

  • ITestListener  测试运行的监听器。

  • IConfigurationListener 监听配置方法相关的接口。

  • IMethodInterceptor 用于修改TestNG即将运行的测试方法列表。

  • IInvokedMethodListener 测试方法拦截监听,用于获取被TestNG调用的在Method的Before 和After方法监听器。该方法只会被配置和测试方法调用。

  • Ihookable 若测试类实现了该接口,当@Test方法被发现时,它的run()方法将会被调用来替代@Test方法。这个测试方法通常在IHookCallBack的callback之上调用,比较适用于需要JASS授权的测试类。例如:

public void run(final IHookCallBack icb, ITestResult testResult) {
   // Preferably initialized in a @Configuration method
   mySubject = authenticateWithJAAs();

   Subject.doAs(mySubject, new PrivilegedExceptionAction() {
     public Object run() {
       icb.callback(testResult);
     }
   };
 }
  • Ireporter 实现该接口可以生成一份测试报告。

  • IRetryAnalyzer 该接口实现重试失败用例。

 

  • 启用监听

  • 使用注解@Listener({监听类.class})

@Listeners({SuiteListener.class})
public class InvokedMethodListenerExample {
    @BeforeSuite
    public void beforeSuite() {
        System.out.println("before suite");
    }
    @Test
    public void t1() {
        System.out.println("t1 test method");
    }
            
    @AfterSuite
    public void afterSuite() {
        System.out.println("after suite");
    } 
}
  • 在TestNG中配置监听类

?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite verbose="0" name="listeners-test">
    <listeners>
        <listener class-name="listeners.ProgressTracker" />
        <listener class-name="listeners.SuitListener" />
        <listener class-name="listeners.RetryListener" />
    </listeners>
    <test name="listeners.testng">
    <packages>
        <package name="listeners" />
    </packages>
   </test>
</suite>
  • 在maven-surefire插件中配置监听类

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>TestNG_${env}.xml</suiteXmlFile>
        </suiteXmlFiles>
        <properties>
            <property>
                <name>listener</name>
                <value>listeners.SuitListener, listeners.RetryListener</value>
            </property>
        </properties>
    </configuration>
</plugin>

TestListenerAdapter 一个基于ITestListener 的简单适配器,存储了被运行的所有测试用例。可以通过如下方法获取到测试结果:

  • getPassedTests()
  • getFailedTests()
  • getSkippedTests()

当你扩展这个类来重载这些方法时,如果你想要这个列表测试Test被维护时,需要调用super的同类。

TestNG Listener

标签:conf   zed   pre   修改   round   org   ges   package   配置   

原文地址:http://www.cnblogs.com/heaven1025/p/6021517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!