当前位置:网站首页>Testng listener
Testng listener
2022-08-03 22:27: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 Listener provides a similar toAOP的方式,对测试methodTo surround the programming.主要的应用场景:
1. Dynamic to rewrite the testmethod (Replace test method is empty running)
2. Dynamically decide whether to skip the testmethod (例如鉴权)
3. 自定义注解,例如@ignore, When detected with the annotation,Skip the test execution
边栏推荐
猜你喜欢

物联网新零售模式,引领购物新潮流

.NET6之MiniAPI(十四):跨域CORS(上)

老板:公司系统太多,能不能实现账号互通?

2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis

全球观之地理部分

【MySQL进阶】数据库与表的创建和管理
![[b01lers2020]Life on Mars](/img/d0/d5c9b7224542c8843ce29adc7ef713.png)
[b01lers2020]Life on Mars

重发布实验报告

On the Qixi Festival of 2022, I will offer 7 exquisite confession codes, and at the same time teach you to quickly change the source code for your own use

21天打卡挑战学习MySQL——《Window下安装MySql》第一周 第三篇
随机推荐
生成器版和查看器版有什么区别?
Embedded Systems: Clocks
Flink--Join以及Flink函数
老板:公司系统太多,能不能实现账号互通?
电商秒杀系统
数据一致性:双删为什么要延时?
utils 定时器
HCIP第十四天
Internet user account information management regulations come into effect today: must crack down on account trading and gray products
Codeup brushing notes - simple simulation
【day6】类与对象、封装、构造方法
UVa 437 - The Tower of Babylon (White Book)
Data_web(八)mysql增量同步到mongodb
图的基础概念
UVa 437 - The Tower of Babylon(白书)
Websocket multi-threaded sending message error TEXT_PARTIAL_WRITING--Use case of spin lock replacing synchronized exclusive lock
直播预告 | 构建业务智联,快速拥抱财务数字化转型
HCIP BGP实验报告
HCIP第十六天
嵌入式开发:嵌入式基础——代码和数据空间揭秘