在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