TestNG中 ITestListener 的使用

1.关于testng中ITestListener 的相关介绍文档,请参考:

http://javadox.com/org.testng/testng/6.8.7/org/testng/ITestListener.html

2.ITestListener 的实际相关应用

 

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

//implements实现接口ITestListener
public class customListener implements ITestListener{
    
    public void onFinish(ITestContext context) {
        System.out.println("finish test");
    }

    public void onStart(ITestContext context) {
        System.out.println("start test");
    }
    
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        System.out.println("Result fail but with success percentage");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.err.println(result.getTestClass().getName() + "." + result.getMethod().getMethodName() + " : failed");
        
    }
    @Override
    public void onTestSkipped(ITestResult result) {
        System.err.println(result.getTestClass().getName() + "." + result.getMethod().getMethodName() + " : skipped");
        
    }


    public void onTestStart(ITestResult result) {
        System.out.println("***start on test***"+ result.getMethod().getMethodName() );

    }

    public void onTestSuccess(ITestResult result) {
        
        System.err.println(result.getTestClass().getName() + "." + result.getMethod().getMethodName() + " : passed");
    }


}

注意:下面方法一和方法二用的是同一个类(customListener )

方法一:采用注释法添加监听设置

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

public class testforCustomList {
	
	
	@Listeners(customListener.class)//此处指明了使用自定义的listener。
	public class listenerTest {

	    @Test
	    public void listener1(){
	        System.out.println("it is the first test");
	    }

	    @Test
	    public void listener2(){
	        System.out.println("it is the second test");
	    }
	}
}

  测试结果如下:

[RemoteTestNG] detected TestNG version 6.8.9
[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-1101982489\testng-customsuite.xml

start test
***start on test***listener1
it is the first test
WebDriverAPI.testforCustomList$listenerTest.listener1 : passed***start on test***listener2

WebDriverAPI.testforCustomList$listenerTest.listener2 : passed
it is the second test
finish test
PASSED: listener1
PASSED: listener2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3b6eb2ec: 50 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@7a0ac6e3: 10 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@43a25848: 80 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@2d6e8792: 10 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@5b464ce8: 10 ms

方法二:使用testng.xml实现监听设置

import org.testng.annotations.Test;

public class testforCustomList {

    public class listenerTest {

        @Test
        public void listener1(){
            System.out.println("it is the first test");
        }

        @Test
        public void listener2(){
            System.out.println("it is the second test");
        }
    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="webTest">
    <listeners>
        <listener class-name="WebDriverAPI.customListener"></listener>
    </listeners>
    <test name="listener test">

        <classes>
            <class name="WebDriverAPI.testforCustomList" />
        </classes>

    </test>
</suite>

运行结果:

[RemoteTestNG] detected TestNG version 6.8.9
[TestNG] Running:
  F:\workplace\webTest\testngXML\listener.xml

start test
***start on test***listener1
it is the first test
WebDriverAPI.testforCustomList$listenerTest.listener1 : passed
WebDriverAPI.testforCustomList$listenerTest.listener2 : passed
***start on test***listener2
it is the second test
finish test

===============================================
webTest
Total tests run: 2, Failures: 0, Skips: 0
===============================================

 

posted @ 2018-02-28 15:17  蓝冰lanbing  阅读(604)  评论(0编辑  收藏  举报