TestNG listeners

本文详细介绍了TestNG监听器的概念及使用方法,包括多种类型的监听器如IAnnotationTransformer、IConfigurationListener等,以及如何创建和应用监听器来修改TestNG的行为,实现自动化测试脚本的定制化报告和日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Testng listener is defined as interface that modifies the default TestNG's behavior.  As the name suggests Listeners "listen" to the event defined the test script and behave accordingly.  It's used in automation test scripts by implementing Listeners Interface.  It allows customizing TestNG reports or logs.  There are many types of TestNG listeners available which allow you to hange the TestNG's behavior.

example listeners:

1. IAnnotationTransfrormer

2. IAnnotationTransformer2

3. IConfigurable

4. IConfigurationListener

5. IExecutionListener

6. IHookable

7. IInvokedMethodListener

8. IInvokedMethodListener2

9. IMethodInterceptor

10. IReporter

11. ISuiteListener

12. ITestListener

 

take ITestListener for example, this listener has following methods:

onStart() - method is called when any test starts

onTestSuccess() - method is called on the success of any test

onTestFailure() - method is called on the failure of any test

onTestSkipped() - method is called on skipped of any test

onTestFailedButWithinSuccessPercentage - method is called each time test fails but is within success percentage.

onFinish() - method is called after all tests are executed.

 

Steps to create a TestNG Listener

1) create class that implements 'ITestListener', and override the defined methods in interface.

public class ListenerTest implements ITestListener                        
{        

    @Override        
    public void onFinish(ITestContext arg0) {                    
        // TODO Auto-generated method stub                
                
    }        

    @Override        
    public void onStart(ITestContext arg0) {                    
        // TODO Auto-generated method stub                
                
    }        

    @Override        
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {                    
        // TODO Auto-generated method stub                
                
    }        

    @Override        
    public void onTestFailure(ITestResult arg0) {                    
        // TODO Auto-generated method stub                
                
    }        

    @Override        
    public void onTestSkipped(ITestResult arg0) {                    
        // TODO Auto-generated method stub                
                
    }        

    @Override        
    public void onTestStart(ITestResult arg0) {                    
        // TODO Auto-generated method stub                
                
    }        

    @Override        
    public void onTestSuccess(ITestResult arg0) {                    
        // TODO Auto-generated method stub                
                
    }        
}        

Then we can modify the methods with your logic.

2) Create test class:

public class TestCases {            
    
@Test        
public void TestToFail()                
{        
    System.out.println("This method to test fail");                    
    Assert.assertTrue(false);            
}        
}

3) Add our listener to the test, the first way is to user Listeners annotation as below, the annotation can be added to a class or a test method.

@Listeners(ListenerTest.class)                

If project has multiple classes adding Listeners to each one of them could be cumbersome and error prone.  In such cases, we can add listeners tag in testng.xml.

 

转载于:https://www.cnblogs.com/tingtwang/p/11098180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值