当前位置:网站首页>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
边栏推荐
猜你喜欢
pikachu Over permission
First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
2022的七夕,奉上7个精美的表白代码,同时教大家快速改源码自用
Go开发工具GoLand V2022.2 来了——Go 工作区重大升级
.NET6之MiniAPI(十四):跨域CORS(上)
嵌入式系统:GPIO
LabVIEW代码生成错误 61056
中国企业构建边缘计算解决方案的最佳实践
随机推荐
什么是memoization,它有什么用?
嵌入式开发:嵌入式基础——代码和数据空间揭秘
UVa 1025 - A Spy in the Metro(白书)
HCIP第十四天
封装、包、访问权限修饰符、static变量
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
云计算国内外发展现状
CAS:1620523-64-9_Azide-SS-biotin_biotin-disulfide-azide
UVa 1025 - A Spy in the Metro (White Book)
HDU 5655 CA Loves Stick
E-commerce data warehouse ODS layer-----log data loading
如何创建一个Web项目
趣链的产品构架
互联网用户账号信息管理规定今起施行:必须严打账号买卖灰产
Golang第二章:程序结构
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
472. Concatenated Words
Bytebase数据库 Schema 变更管理工具
Golang Chapter 2: Program Structure
[MySQL Advanced] Creation and Management of Databases and Tables