当前位置:网站首页>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, 当检测到有这个注解的时候,跳过测试执行
边栏推荐
猜你喜欢

LabVIEW code generation error 61056

October 2019 Twice SQL Injection

Bytebase数据库 Schema 变更管理工具
![[b01lers2020]Life on Mars](/img/d0/d5c9b7224542c8843ce29adc7ef713.png)
[b01lers2020]Life on Mars

2022-08-02 mysql/stonedb慢SQL-Q18-内存使用暴涨分析

noip初赛

如何设计 DAO 的 PoW 评判标准 并平衡不可能三角

嵌入式系统:GPIO
Causes of Mysql Disk Holes and Several Ways to Rebuild Tables

113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节
随机推荐
如何基于WPF写一款数据库文档管理工具(二)
Codeup刷题笔记-简单模拟
数据一致性:双删为什么要延时?
October 2019 Twice SQL Injection
Adobe是什么?
CAS:122567-66-2_DSPE-生物素_DSPE-Biotin
Embedded Systems: Clocks
CAS:1620523-64-9_Azide-SS-biotin_biotin-disulfide-azide
pikachu Over permission 越权
Embedded Systems: GPIO
UVa 10003 - Cutting Sticks (White Book, Interval DP)
Causes of Mysql Disk Holes and Several Ways to Rebuild Tables
win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
Cisco ike2 IPSec configuration
VIM操作
Boss: There are too many systems in the company, can you realize account interoperability?
软件测试人员必备的60个测试工具清单,建议收藏一波~
超级实用网站+公众号合集
CAS: 773888-45-2_BIOTIN ALKYNE_生物素-炔基
CAS:1797415-74-7_TAMRA-Azide-PEG-Biotin