第二十一章 TestNG-Listeners

本文详细介绍了TestNG中的IInvokedMethodListener、ITestListener和ISuiteListener三种监听器的使用方法,包括在测试方法执行前后的回调、测试开始和结束、套件启动和结束等关键节点的监听。同时,展示了如何将这些监听器进行整合,实现对整个测试流程的全面监控。示例代码展示了监听器如何影响测试输出并捕获异常信息。
摘要由CSDN通过智能技术生成

第二十一章 TestNG-Listeners

1.IInvokedMethodListener(监听方法)
package ListenerDemo;

import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import Listener.CustomListener1;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/22
 */
@Listeners(CustomListener1.class)//通过注释调用监听器
public class ListenerTest {
    @Test(alwaysRun = true)
    public void test() {
        System.out.println("test()");
        Assert.assertTrue(false);
    }

    @Test
    public void test2() {
        System.out.println("test2()");
        Assert.assertTrue(true);
    }
}

监听器:

package Listener;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/22
 */
public class CustomListener1 implements IInvokedMethodListener {
    @Override
    public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        System.out.println("beforeInvocation:" + iTestResult.getTestClass().getName() + "-->" + iInvokedMethod.getTestMethod().getMethodName());
    }

    @Override
    public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        System.out.println("beforeInvocation" + iTestResult.getTestClass().getName() + "-->"  + iInvokedMethod.getTestMethod().getMethodName());
    }
}

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test">
    <test name="Application name" preserve-order="false">
        <classes>
            <class name="ListenerDemo.ListenerTest"></class>
        </classes>
    </test>
</suite>

运行结果:

beforeInvocation:ListenerDemo.ListenerTest-->test
test()
beforeInvocationListenerDemo.ListenerTest-->test

java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual   :false

beforeInvocation:ListenerDemo.ListenerTest-->test2
test2()
beforeInvocationListenerDemo.ListenerTest-->test2
===============================================
Regression Test
Total tests run: 2, Failures: 1, Skips: 0
===============================================
Process finished with exit code 0
2.ITestListener(监听Test)
package ListenerDemo;

import Listener.CustomListener1;
import Listener.CustomListener2;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/22
 */
@Listeners(CustomListener2.class)
public class ListenerTest1 {
    @Test(alwaysRun = true)
    public void test() {
        System.out.println("test()");
        Assert.assertTrue(false);
    }

    @Test
    public void test2() {
        System.out.println("test2()");
        Assert.assertTrue(true);
    }

}
package Listener;

import org.testng.*;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/22
 */
public class CustomListener2 implements ITestListener {

    @Override
    public void onTestStart(ITestResult iTestResult) {
        System.out.println("onTestStart:"+iTestResult.getName());
    }

    @Override
    public void onTestSuccess(ITestResult iTestResult) {
        System.out.println("onTestSuccess:"+"-->"+iTestResult.getTestClass().getName());
    }

    @Override
    public void onTestFailure(ITestResult iTestResult) {
        System.out.println("onTestFailure:"+"-->"+iTestResult.getTestClass().getName());

    }

    @Override
    public void onTestSkipped(ITestResult iTestResult) {

    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {

    }

    @Override
    public void onStart(ITestContext iTestContext) {
        System.out.println("onStart:"+iTestContext.getName());
        ITestNGMethod[] iTestNGMethods=iTestContext.getAllTestMethods();
        for (ITestNGMethod iTestNGMethod:iTestNGMethods){
            System.out.println(iTestNGMethod.getMethodName());
        }
    }

    @Override
    public void onFinish(ITestContext iTestContext) {
        System.out.println("onFinish:"+iTestContext.getName());
    }
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test">
    <test name="Application name" preserve-order="false">
        <classes>
            <class name="ListenerDemo.ListenerTest1"></class>
        </classes>
    </test>
</suite>

运行结果:

onStart:Application name
test
test2
onTestStart:test

test()
onTestFailure:-->ListenerDemo.ListenerTest1

java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual   :false

onTestStart:test2

test2()
onTestSuccess:-->ListenerDemo.ListenerTest1

onFinish:Application name

===============================================
Regression Test
Total tests run: 2, Failures: 1, Skips: 0
===============================================


Process finished with exit code 0

3.ISuiteListener(监听套件)
package ListenerDemo;

import Listener.CustomListener2;
import Listener.ISuiteListener;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/22
 */
//@Listeners(ISuiteListener.class)
public class ListenerTest2 {
    @Test(alwaysRun = true)
    public void test() {
        System.out.println("test()");
        Assert.assertTrue(false);
    }

    @Test
    public void test2() {
        System.out.println("test2()");
        Assert.assertTrue(true);
    }

}
package Listener;

import org.testng.*;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/22
 */
public class ISuiteListener implements org.testng.ISuiteListener {
    @Override
    public void onStart(ISuite iSuite) {
        System.out.println("onStart:"+iSuite.getName());
    }

    @Override
    public void onFinish(ISuite iSuite) {
        System.out.println("onFinish:"+iSuite.getName());
    }
}

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test">
    <listeners>
        <listener class-name="Listener.ISuiteListener"></listener>
    </listeners>
    <test name="Application name" preserve-order="false">
        <classes>
            <class name="ListenerDemo.ListenerTest2"></class>
        </classes>
    </test>
</suite>

运行结果:

onStart:Regression Test
test()

java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual   :false
test2()
onFinish:Regression Test
===============================================
Regression Test
Total tests run: 2, Failures: 1, Skips: 0
===============================================
Process finished with exit code 0
4.监听器代码整合
package Listener;

import org.testng.*;
import org.testng.ISuiteListener;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/22
 */
public class CustomListener implements IInvokedMethodListener, ISuiteListener,ITestListener {
    @Override
    public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        System.out.println("beforeInvocation:" + iTestResult.getTestClass().getName() + "-->" + iInvokedMethod.getTestMethod().getMethodName());
    }

    @Override
    public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        System.out.println("beforeInvocation" + iTestResult.getTestClass().getName() + "-->"  + iInvokedMethod.getTestMethod().getMethodName());
    }
    @Override
    public void onTestStart(ITestResult iTestResult) {
        System.out.println("onTestStart:"+iTestResult.getName());
    }

    @Override
    public void onTestSuccess(ITestResult iTestResult) {
        System.out.println("onTestSuccess:"+"-->"+iTestResult.getTestClass().getName());
    }

    @Override
    public void onTestFailure(ITestResult iTestResult) {
        System.out.println("onTestFailure:"+"-->"+iTestResult.getTestClass().getName());

    }

    @Override
    public void onTestSkipped(ITestResult iTestResult) {

    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {

    }

    @Override
    public void onStart(ITestContext iTestContext) {
        System.out.println("onStart:"+iTestContext.getName());
        ITestNGMethod[] iTestNGMethods=iTestContext.getAllTestMethods();
        for (ITestNGMethod iTestNGMethod:iTestNGMethods){
            System.out.println(iTestNGMethod.getMethodName());
        }
    }

    @Override
    public void onFinish(ITestContext iTestContext) {
        System.out.println("onFinish:"+iTestContext.getName());
    }
    @Override
    public void onStart(ISuite iSuite) {
        System.out.println("onStart:"+iSuite.getName());
    }

    @Override
    public void onFinish(ISuite iSuite) {
        System.out.println("onFinish:"+iSuite.getName());
    }
}

直接调用一个就可以了

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test">
    <listeners>
        <listener class-name="Listener.CustomListener"></listener>
    </listeners>
    <test name="Application name" preserve-order="false">
        <classes>
            <class name="ListenerDemo.ListenerTest1"></class>
        </classes>
    </test>
</suite>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值