Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a failed retry of a use case by TestNG in a custom Listener form

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Failed to implement use case in label form and retry

The previous article explained how to achieve a failed retry of a use case in the form of a tag, @ Test (retryAnalyzer = MyRetry.class).

For more information, please refer to the article: explain the use case failure retry and custom listeners in TestNG

However, there are still some problems with the @ Test tag, that is, this method can only be around the class or method. When there are many test classes, you need to add the tag @ Test (retryAnalyzer = MyRetry.class) in front of each class.

Is there any overall way to fix this? We can solve this problem by customizing listener!

Failed to implement use case in custom Listener format and retry

We have implemented the retry mechanism for the use case failure through the tag, so we can write a listenser to control the tag. IAnnotationTransformer is provided in TestNG to let us achieve this goal. Don't say much, just go to the code.

The code implementation of the retry code logic is as follows:

Import org.testng.IRetryAnalyzer; import org.testng.ITestResult; public class MyRetry implements IRetryAnalyzer {private int retryCount = 0; private static final int maxRetryCount = 3X / use case failed re-execution of @ Override public boolean retry (ITestResult result) {if (retryCount < maxRetryCount) {retryCount++; return true;} return false;}}

The listener code implementation of custom Retry is as follows:

Import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.testng.IAnnotationTransformer; import org.testng.annotations.ITestAnnotation; public class RetryListener implements IAnnotationTransformer {@ Override public void transform (ITestAnnotation testannotation, Class testClass, Constructor testConstructor, Method testMethod) {testannotation.setRetryAnalyzer (MyRetry.class);}}

The test class code is as follows:

Import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; public class Case1 {@ Test public void f1 () {System.out.println ("f11"); assertEquals ("a", "b");} @ Test public void f2 () {System.out.println ("f21"); assertEquals ("a", "a");}}

Set the configuration file for testng and add a custom RetryListener

The result of the run is as follows, you can see that it has been retried 3 times.

Note: if in a case, there is a tag retry mechanism in the method as follows (set retry to 1), what should I do?

@ Test (retryAnalyzer = MyRetry2.class) public void F1 () {System.out.println ("f11"); assertEquals ("a", "b");}

When we run the code, we will find that the main thing is to add the MyRetry.class used in the custom RetryListener in the configuration file of testng.

This is the answer to the question about how TestNG realizes the failed retry of the use case through custom Listener. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report