在testng中大部分的注解已经能够知足咱们测试的需求,可是在测试的时候想要经过注解的方式加入本身测试一些内容,好比 测试项目 测试描述 验证点等信息,可经过自定义注解的方式实现。java
具体操做步骤以下:maven
1.建立maven工程ide
自行查询建立maven工程的方法测试
2.pom文件中引入testng依赖ui
<!-- 配置testng依赖 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
</dependency>
3.建立自定义注解类spa
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.建立监听blog
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.建立测试类,并引用Listenerip
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中引用监听的方式有不少种,采用一种便可get