当前位置:网站首页>Testng监听器
Testng监听器
2022-08-03 22:18:00 【qq_492448446】
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="API" parallel="none">
<listeners>
<listener class-name="com.welab.automation.framework.listener.RetryListener"></listener>
</listeners>
<test verbose="2" preserve-order="true" name="channel">
<classes>
<class name="com.welab.automation.projects.channel.runners.MobileTestRunner_iOS">
<methods>
<include name="runScenario"/>
</methods>
</class>
</classes>
</test>
</suite>
import com.welab.automation.framework.uils.PropertiesReader;
import org.testng.ITestResult;
import org.testng.util.RetryAnalyzerCount;
public class TestRetryAnalyzer extends RetryAnalyzerCount {
private static int count =1;
public TestRetryAnalyzer() {
super.setCount(count);
}
@Override
public boolean retryMethod(ITestResult result) {
return true;
}
public void reSetCount() {
super.setCount(count);
}
}
import com.welab.automation.framework.utils.entity.api.JsonEntity;
import com.welab.automation.framework.utils.entity.api.TestCaseUtils;
import org.testng.*;
import org.testng.annotations.ITestAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class RetryListener extends TestListenerAdapter implements IAnnotationTransformer,IHookable {
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
IRetryAnalyzer iRetryAnalyzer = annotation.getRetryAnalyzer();
if (iRetryAnalyzer == null) {
annotation.setRetryAnalyzer(TestRetryAnalyzer.class);
}
}
@Override
public void onFinish(ITestContext testContext) {
Iterator<ITestResult> listOfSkippedTests =
testContext.getSkippedTests().getAllResults().iterator();
while (listOfSkippedTests.hasNext()) {
ITestResult skippedTest = listOfSkippedTests.next();
ITestNGMethod method = skippedTest.getMethod();
if (testContext.getFailedTests().getResults(method).size() > 0
|| testContext.getPassedTests().getResults(method).size() > 0) {
skippedTest.setStatus(0);
listOfSkippedTests.remove();
}
}
}
//iHookable重写方法run
@Override
public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) {
JsonEntity jsonEntity = getJsonEntity(iTestResult);
Map<String, List<String>> sqlExpression = null;
//replace parameter
if (jsonEntity != null) {
jsonEntity.setJsonObject(TestCaseUtils.replaceParameter(jsonEntity.getJsonObject()));
jsonEntity.updateHeader();
}
//execute test case
iHookCallBack.runTestMethod(iTestResult);
}
public JsonEntity getJsonEntity(ITestResult testResult) {
JsonEntity jsonEntity = null;
Object[] parameters = testResult.getParameters();
if (parameters.length > 0) {
for (Object parameter : parameters) {
if (parameter instanceof JsonEntity) {
jsonEntity = (JsonEntity) parameter;
}
}
}
return jsonEntity;
}
@Override
public void onTestFailure(ITestResult result) {
TestRetryAnalyzer testRetryAnalyzer = (TestRetryAnalyzer) result.getMethod().getRetryAnalyzer();
testRetryAnalyzer.reSetCount();
}
}
IHookable 监听器提供一种类似于AOP的方式,对测试method进行环绕编程。主要的应用场景:
1. 动态重写测试method (替换测试方法为空运行)
2. 动态决定是否跳过测试method (例如鉴权)
3. 自定义注解,例如@ignore, 当检测到有这个注解的时候,跳过测试执行
边栏推荐
猜你喜欢
【历史上的今天】8 月 3 日:微软研究院的创始人诞生;陌陌正式上线;苹果发布 Newton OS
【刷题篇】二叉树的右视图
encapsulation, package, access modifier, static variable
LVS负载均衡集群
【bug】汇总Elipse项目中代码中文乱码解决方法!
七夕快乐!
物联网新零售模式,引领购物新潮流
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
2022的七夕,奉上7个精美的表白代码,同时教大家快速改源码自用
Flutter 桌面探索 | 自定义可拖拽导航栏
随机推荐
LitJson报错记录
趣链的产品构架
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
【刷题篇】二叉树的右视图
封装、包、访问权限修饰符、static变量
CAS:1260586-88-6_生物素-C5-叠氮_Biotin-C5-Azide
HCIP第十五天
LabVIEW代码生成错误 61056
386. Lexicographical Numbers
嵌入式系统:时钟
【进阶自动化测试】一文1000教你如何用Postman做接口自动化测试
CAS: 773888-45-2_BIOTIN ALKYNE_Biotin-alkynyl
[N1CTF 2018] eating_cms
With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
Bytebase数据库 Schema 变更管理工具
图的基础概念
Unification of east-west and north-south communications
Optimize the query (work in progress)
376. Wiggle Subsequence
JPA Native Query(本地查询)及查询结果转换