Chapter sixteen, 9-Listeners Listener

A, IInokedMethodListener

1, to achieve a class to monitor the operation of the testcase.

. 1  Package listenerspackage;
 2  
. 3  Import org.testng.IInvokedMethod;
 . 4  Import org.testng.IInvokedMethodListener;
 . 5  Import org.testng.ITestResult;
 . 6  
. 7  / ** 
. 8  * listening * / 
. 9  
10  // All methods by listening Interface provided, so we need to implement the interface 
11  public  class CustomerListeners1 the implements IInvokedMethodListener {
 12      
13  //     beforeInvocation and afterInvocation these two methods in the interface has been written, we need to follow the requirements described in the main part of the full
 14  
15  //     occur before calling performed before each method in the class testcase run
16      @Override
 . 17      public  void beforeInvocation (Method IInvokedMethod, ITestResult TestResult) {
 18 is  //         .getTestClass (): returns the test method where the test class, a class type
 . 19  //         .getName (): returns the class name of the class 
20 is          the System .out.println ( "before the invocation:." + testResult.getTestClass () getName () + "->" + . method.getTestMethod () getMethodName ());
 21 is      }
 22 is  
23 is  //     occurs after the call, the class testcase each method is performed after a run 
24      @Override
 25      public  void afterInvocation (method IInvokedMethod, ITestResult TestResult) {
 26 is         System.out.println("after Invocation:"+testResult.getTestClass().getName()+" -> "+method.getTestMethod().getMethodName());
27     }
28 }

2, create a class that is listening

 1 package testclasses1;
 2 
 3 import org.testng.Assert;
 4 import org.testng.annotations.AfterClass;
 5 import org.testng.annotations.BeforeClass;
 6 import org.testng.annotations.Listeners;
 7 import org.testng.annotations.Test;
 8 
 9 import listenerspackage.CustomerListeners1;
10 
11 //表示该测试类被CustomerListeners1类监听
12 @Listeners(CustomerListeners1.class)
13 public class TestNG_ListenersTest1 {
14     
15     @BeforeClass
16     public void setUp() {
17         System.out.println("Code in before class");
18     }
19     
20     @AfterClass
21     public void classUp() {
22         System.out.println("Code in after class");
23     }
24     
25   @Test
26   public void testMethod1() {
27         System.out.println("Code in testMethod1");
28         Assert.assertTrue(true);
29   }
30   
31   @Test
32   public void testMethod2() {
33         System.out.println("Code in testMethod2");
34         Assert.assertTrue(false);
35   }
36 }

 3, the configuration xml

1 <!-- 没有此行配置运行时可能会报错 -->
2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3 <suite name="Listeners TestSuite">
4     <test name="Application Test">
5         <classes>
6             <class name="testclasses1.TestNG_ListenersTest1"></class>
7         </classes>
8     </test>
9 </suite>

4, operating results

Plus the listener is not running before results:

After adding the listener operating results:

 

 

Two, ITestListener

1, to achieve ITestListener interface to listen for the operation of the testcase.

. 1  Package listenerspackage;
 2  
. 3  Import org.testng.ITestContext;
 . 4  Import org.testng.ITestListener;
 . 5  Import org.testng.ITestNGMethod;
 . 6  Import org.testng.ITestResult;
 . 7  
. 8  / ** 
. 9  * listening * / 
10  
. 11  / / All methods provide an interface by listening, so we need to implement the interface 
12 is  public  class CustomerListeners2 the implements ITestListener {
 13 is  
14      @Override
 15      public  void onTestStart (ITestResult Result) {
 16 //         test method to begin when this method (method annotated with @test)
 17  //         .getName () returns the name of the test method 
18          System.out.println ( "onTestStart -> the Test name:" + the Result. getName ());        
 . 19      }
 20 is  
21 is      @Override
 22 is      public  void onTestSuccess (ITestResult Result) {
 23 is  //         method to run only after successful testing method performed (annotated with @test) 
24          System.out.println ( "onTestSuccess - > Test name: "+ result.getName ());            
 25      }
 26 is  
27      @Override
 28      public  void onTestFailure (ITestResult Result) {
29  //         (annotated Method @test band) is performed only after the test method fails in 
30          System.out.println ( "onTestSuccess -> Test name:" + result.getName ());        
 31 is      }
 32  
33 is      @Override
 34 is      public  void onTestSkipped (ITestResult Result) {
 35  //         performed while skipping the test 
36      }
 37 [  
38 is      @Override
 39      public  void onTestFailedButWithinSuccessPercentage (ITestResult Result) {
 40  //         test fails, but only in the percentage of successful execution (automation not used) 
41 is      }
 42 is  
43 is      @Override
 44 is     public  void the onStart (ITestContext context) {
 45  //         performed test tag xml configuration file before running
 46 is  //         .getName () returns the name of the tag 
47          System.out.println ( "the onStart -> test Tag name : "+ context.getName ());    
 48  //         returns the names of all of the test methods 
49          ITestNGMethod methods [] = context.getAllTestMethods ();
 50          System.out.println (" test test method to be executed in the label: " ) ;
 51 is          for (ITestNGMethod Method: Methods) {
 52 is              System.out.println (method.getMethodName ());
 53 is          }
 54 is      }
 55 
56 is      @Override
 57 is      public  void onFinish (ITestContext context) {
 58  //         After the test run the tag is performed in the xml configuration file 
59          System.out.println ( "onFinish -> test Tag name:" + context.getName ()) ;        
 60      }
 61 }

2, the class monitor

 1 package testclasses1;
 2 
 3 import org.testng.Assert;
 4 import org.testng.annotations.AfterClass;
 5 import org.testng.annotations.BeforeClass;
 6 import org.testng.annotations.Listeners;
 7 import org.testng.annotations.Test;
 8 
 9 import listenerspackage.CustomerListeners1;
10 
11 //表示该测试类被CustomerListeners1类监听
12 @Listeners(CustomerListeners1.class)
13 public class TestNG_ListenersTest2 {
14     
15     @BeforeClass
16     public void setUp() {
17         System.out.println("Code in before class");
18     }
19     
20     @AfterClass
21     public void classUp() {
22         System.out.println("Code in after class");
23     }
24     
25   @Test
26   public void testMethod1() {
27         System.out.println("Code in testMethod1");
28         Assert.assertTrue(true);
29   }
30   
31   @Test
32   public void testMethod2() {
33         System.out.println("Code in testMethod2");
34         Assert.assertTrue(false);
35   }
36 }

3, xml configuration

 1 <!-- 没有此行配置运行时可能会报错 -->
 2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
 3 <suite name="Listeners TestSuite">
 4     <test name="Application Test">
 5         <classes>
 6             <class name="testclasses1.TestNG_ListenersTest2"></class>
 7         </classes>
 8     </test>
 9     <test name="Application Test">
10         <classes>
11             <class name="testclasses1.TestNG_ListenersTest2"></class>
12         </classes>
13 </suite>

4, operating results

 

Three, ISuiteListener

. 1  Package listenerspackage;
 2  
. 3  Import org.testng.IInvokedMethod;
 . 4  Import org.testng.IInvokedMethodListener;
 . 5  Import org.testng.ISuite;
 . 6  Import org.testng.ISuiteListener;
 . 7  Import org.testng.ITestResult;
 . 8  
. 9  / ** 
10  * listening * / 
11  
12  // all methods of listening by the interfaces provided, so we need to implement the interface 
13  public  class CustomerListeners3 the implements ISuiteListener {
 14  
15  //     ago xml file suite tag content started when
16      @Override
 . 17      public  void the onStart (iSUITE suite) {
 18 is          System.out.println ( "begin before the onStart suite" );
 19      }
 20 is  
21 is  //     When suite xml file label contents after execution 
22 is      @Override
 23 is      public  void onFinish (iSUITE Suite) {
 24          System.out.println ( "after the execution onFinish suite" );
 25      }    
 26 }

 

Fourth, when we will listen to the class that implements the interface by, if you want to refer to the test class, there are two ways:

1, requires the use of annotations: @Listeners (implementation class name .class monitor interface)

 2, configured in the xml configuration file

1 <! - this may be given when there is no run-line configuration ->
 2 <DOCTYPE the SYSTEM Suite "http://testng.org/testng-1.0.dtd"!>
 . 3 <Suite name = "the TestSuite the Listeners">
 . 4 <! - Specifies the listener method CustomerListeners3 suite class referenced in the format: package name class name -.> 
 . 5 <the listeners> 
 . 6 <listener class-name = "listenerspackage.CustomerListeners3"> </ listener> 
 . 7 </ the listeners > 
. 8      <Test name = "Test1 the Application">
 . 9          <classes>
 10              < class name = "testclasses1.TestNG_ListenersTest1"> </ class >
 . 11          </ classes>
12     </test>
13     <test name="Application Test2">
14         <classes>
15             <class name="testclasses1.TestNG_ListenersTest2"></class>
16         </classes>
17     </test>
18 </suite>

 

 

 

 

If you do not understand the small partners can be added to group "555 191 854" I asked, the group is small software industry partners together to learn from each other.

Content with coherence, unlabeled place to see the previous blog, which is a set of automated content on ava selenium +, starting with java foundation.

Welcome attention, please indicate the source.

Guess you like

Origin www.cnblogs.com/luohuasheng/p/11535810.html