当前位置:网站首页>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
边栏推荐
- E-commerce data warehouse ODS layer-----log data loading
- 藏宝计划TreasureProject(TPC)系统模式开发技术原理
- Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo
- 385. Mini Parser
- 图的基础概念
- 目标检测的国内外研究现状
- 用于流动质押和收益生成的 Web3 基础设施
- Codeup刷题笔记-简单模拟
- MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
- Nine ways to teach you to read the file path in the resources directory
猜你喜欢
随机推荐
node连接mysql数据库报错:Client does not support authentication protocol requested by server
PowerMockup 4.3.4::::Crack
Adobe是什么?
RPA助力商超订单自动化!
老板:公司系统太多,能不能实现账号互通?
全球观之地理部分
With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
HCIP第十三天
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
【云原生实用技巧】使用 skopeo 批量同步 helm chart 依赖镜像
Makefile
一些思考:腾讯股价为何持续都低
中国企业构建边缘计算解决方案的最佳实践
websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
382. Linked List Random Node
LabVIEW代码生成错误 61056
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
480. Sliding Window Median
MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
Work Subtotal QT Packing