zoukankan      html  css  js  c++  java
  • testng自定义注解

    在testng中大部分的注解已经可以满足我们测试的需求,但是在测试的时候想要通过注解的方式加入自己测试一些内容,比如 测试项目 测试描述  验证点等信息,可通过自定义注解的方式实现。

    具体操作步骤如下:

    1.创建maven工程

       自行查询创建maven工程的方法

    2.pom文件中引入testng依赖

       

        <!-- 配置testng依赖 -->
         <dependency>
           <groupId>org.testng</groupId>
           <artifactId>testng</artifactId>
           <version>6.9.8</version>
         </dependency>
    

      

    3.创建自定义注解类

    package com.test.annotation;
    
    import java.lang.annotation.ElementType;  
    import java.lang.annotation.Retention;  
    import java.lang.annotation.RetentionPolicy;  
    import java.lang.annotation.Target;  
      
    @Retention(RetentionPolicy.RUNTIME)  
    @Target(ElementType.METHOD)  
    public @interface TestDescription {
    	//测试项
        public String item() default "";  
        //测试描述
        public String description() default "";
        //验证点
        public String verification() default "";
    } 
    

      

    4.创建监听

    package com.test.annotation;
    
    import org.testng.IInvokedMethod;  
    import org.testng.IInvokedMethodListener;  
    import org.testng.ITestContext;  
    import org.testng.ITestListener;  
    import org.testng.ITestNGMethod;  
    import org.testng.ITestResult;  
      
    public class MyAnnotationListener implements IInvokedMethodListener, ITestListener {  
    	
    	public String item;  
        public String description;
        public String verification;  
      
        public void onTestStart(ITestResult result) {  
            System.out.println("onTestStart");  
            item = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(TestDescription.class).item(); 
            description = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(TestDescription.class).description();
            verification = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(TestDescription.class).verification();
            System.out.println("item: " + item + " description: " + description);
            System.out.println("verification: " + verification);  
        }  
      
        public void onTestSuccess(ITestResult result) {  
            System.out.println("onTestSuccess");  
      
      
        }  
      
        public void onTestFailure(ITestResult result) {  
            System.out.println("onTestFailure");  
      
      
        }  
      
        public void onTestSkipped(ITestResult result) {  
            System.out.println("onTestSkipped");  
      
      
        }  
      
        public void onStart(ITestContext context) {  
            System.out.println("onStart");  
            for(ITestNGMethod m1 : context.getAllTestMethods()) {  
                if(m1.getConstructorOrMethod().getMethod().isAnnotationPresent(TestDescription.class)) {  
                	item = m1.getConstructorOrMethod().getMethod().getAnnotation(TestDescription.class).item();
                    description = m1.getConstructorOrMethod().getMethod().getAnnotation(TestDescription.class).description();
                    verification = m1.getConstructorOrMethod().getMethod().getAnnotation(TestDescription.class).verification(); 
                    System.out.println("onStart_item:"+item);
                    System.out.println("onStart_description:"+description);
                    System.out.println("onStart_verification:"+verification);
                }  
            }  
      
        }  
      
        public void onFinish(ITestContext context) {  
            System.out.println("onFinish");  
      
      
        }
    
    	@Override
    	public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    		// TODO Auto-generated method stub
    		
    	}  
     
    } 
    

      

    5.创建测试类,并引用Listener

    package com.test.annotation;
    
    import org.testng.annotations.Listeners;
    import org.testng.annotations.Test;
    
    @Listeners(com.test.annotation.MyAnnotationListener.class)  
    public class TestMyAnnotationListener {  
          
        @TestDescription(item = "测试项1", description="描述1;",verification="验证1")  
        @Test  
        public void test001(){  
            System.out.println("运行test001");  
        }
        
        @TestDescription(item = "测试项2", description="描述2;",verification="验证2")  
        @Test 
        public void test002(){
        	System.out.println("运行test002");
        }
    }  
    
    @Listeners(com.test.annotation.MyAnnotationListener.class)  此行代码为引用监听

    6.运行测试类结果如下:
    onStart
    onStart_item:测试项1
    onStart_description:描述1;
    onStart_verification:验证1
    onStart_item:测试项2
    onStart_description:描述2;
    onStart_verification:验证2
    onTestStart
    item: 测试项1 description: 描述1;
    verification: 验证1
    运行test001
    onTestSuccess
    onTestStart
    item: 测试项2 description: 描述2;
    verification: 验证2
    运行test002
    onTestSuccess
    onFinish
    PASSED: test001
    PASSED: test002
    
    ===============================================
        Default test
        Tests run: 2, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================
    Default suite
    Total tests run: 2, Failures: 0, Skips: 0
    ===============================================
    
    [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@3551a94: 6 ms
    [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
    [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@614c5515: 25 ms
    [TestNG] Time taken by org.testng.reporters.jq.Main@6be46e8f: 29 ms
    [TestNG] Time taken by org.testng.reporters.EmailableReporter2@7225790e: 3 ms
    [TestNG] Time taken by org.testng.reporters.XMLReporter@6537cf78: 5 ms
    

    testng中引用监听的方式有很多种,采用一种即可

     

  • 相关阅读:
    PostgreSQL安装的后续工作
    进程在Linux上实际需要多少内存
    PostgreSQL的大对象以及空间使用 (3)
    PostgreSQL的大对象以及空间使用 (2)
    PostgreSQL的大对象以及空间使用 (1)
    PostgreSQL的WAL(4)--WAL创建和调优
    Cannot set Column 'EffectiveDate' to be null. Please use DBNull instead.
    除去datatable的空行
    sql like in 语句获取以逗号分割的字段内的数据
    el-cascader 设置选中任意一级
  • 原文地址:https://www.cnblogs.com/lincj/p/7016210.html
Copyright © 2011-2022 走看看