• TestNG Listener


    • 常用接口

    • 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的同类。

  • 相关阅读:
    Mac连接远程Linux管理文件(samba)
    centos6 防火墙iptables操作整理
    安装postgresql
    linux CentOS6.5 yum安装mysql 5.6
    centos--git搭建之Gogs安装
    查看mysql 默认端口号和修改端口号
    centos之mysql安装配置使用
    流媒体服务器SRS部署
    redis配置文件参数说明
    redis 安装
  • 原文地址:https://www.cnblogs.com/heaven1025/p/6021517.html
Copyright © 2020-2023  润新知