This is the 26th day of my participation in the August Text Challenge.More challenges in August
In our actual work, sometimes use case failure is not caused by interface error, so we need to retry after use case failure, reduce the use case execution failure caused by environment or special circumstances, and reduce false positives in the process. We’ve been looking at Python, but today we’re going to look at Java, and we’re going to look at how to override use case retries in testNG.
We can look at the testNG line of code, and we can find clues that we need to rewrite,
Retrying the IRetryAnalyzer class will implement the retry execution of our use case, and we’ll see how we do it.
public class TestNGRetry implements IRetryAnalyzer {
private int retryCount = 1;
private static final int maxRetryCount = 3;
@Override
public boolean retry(ITestResult result) {
if (retryCount<=maxRetryCount){
retryCount++;
return true;
}
return false;
}
public void reSetCount(a){
retryCount=1; }}Copy the code
When used, on the test method
@Test(retryAnalyzer= TestNGRetry.class)
You can write a listener and put it in the XML configuration so that all test cases can use this retry method for future applications.
public class RetryListener implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retryAnalyzer = annotation.getRetryAnalyzer();// Get the annotation of retryAnalyzer
if (retryAnalyzer == null){ annotation.setRetryAnalyzer(TestNGRetry.class); }}}Copy the code
So how do we write our configuration file, which is shown below
Config file <? xml version="1.0" encoding="UTF-8"? > <suite name="Suite" parallel="false" thread-count="2">
<listeners>
<listener
class-name="chongshi.tesng.TestRunnerListener" />
<listener class-name="chongshi.tesng.RetryListener"/ > < /listeners>
<test name="Test">
<classes>
<class name="chongshi.tesng.New"/ > < /classes>
</test> <! --Test--> </suite> <! -- Suite -->Copy the code
So we can do it in the configuration file.