第二十一章 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>