如何避免在测试注释中添加重试?

huangapple go评论68阅读模式
英文:

How to avoid Retry adding in Test annotation?

问题

utilities/Retry

public class Retry implements IRetryAnalyzer {

     private int retryCount = 0;
     private int maxRetryCount = 2;
     
     public boolean retry(ITestResult result) {
         if(retryCount < maxRetryCount) {
             retryCount ++ ;
             return true;
         }
         return false;
     }
}

tests/RepaymentCalculatorTest

public class RepaymentCalculatorTest extends BaseTest {
    
    Retry retryTest = new Retry();
    //@Test(enabled = true)
    @Test(retryAnalyzer = Retry.class)
    public void loanRepaymentCalculator() throws InterruptedException {

     // 在这里添加其余的UI测试代码....
}
英文:

I came across an answer on how to use Retry in Selenium web driver test. Now I have implemented Retry class under utilities and inside the test annotation given as follows @Test(retryAnalyzer = Retry.class). This is working fine now, but I would like to change this way, can I use the Retry in any other way instead of giving in test annotation as @Test(retryAnalyzer = Retry.class) ? Also I need to comment the @Test(enabled = true), Can someone please advise ?

utilities/Retryclass

public class Retry implements IRetryAnalyzer {

	 private int retryCount = 0;
	 private int maxRetryCount = 2;
	 
	 public boolean retry(ITestResult result) {
		 if(retryCount &lt; maxRetryCount) {
			 retryCount ++ ;
			 return true;
		 }
		 return false;
	 }
}

tests/RepaymentCalculatorTest

public class RepaymentCalculatorTest extends BaseTest {
	
	Retry retryTest = new Retry();
	//@Test(enabled = true)
	@Test(retryAnalyzer = Retry.class)
	public void loanRepaymentCalculator() throws InterruptedException {

     // rest of the UI test code added here ....
}

答案1

得分: 1

我正在使用稍微不同的方式无需在@Test注释中添加类

让我们为其编写自定义的监听器

public class RetryFailedTestCases implements IRetryAnalyzer, IAnnotationTransformer {

    public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor,
            Method testMethod) {
        testannotation.setRetryAnalyzer(RetryFailedTestCases.class);
    }
    
    int counter = 0;
    int retryLimit = 1; //根据需要而定

    @Override
    public boolean retry(ITestResult result) {
        
        if(counter < retryLimit)
        {
            counter++;
            return true;
        }
        
        return false;
    }

}

现在在testng.xml文件中添加这个

<listeners>
  <listener class-name="package.RetryFailedTestCases"/>
</listeners>

当我们作为套件运行时这很简单且有用... 或者我们可以在每个类的顶部提到监听器类而不是testng.xml文件中

我不确定为什么要明确指定enabled=true即使没有@Test也会执行对吗如果你不想执行那就写成false
英文:

i am using bit different way, no need to add class in @Test annotation

Lets have custom Listener for it

public class RetryFailedTestCases implements IRetryAnalyzer, IAnnotationTransformer{

public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor,
		Method testMethod) {
	testannotation.setRetryAnalyzer(RetryFailedTestCases.class);
}

int counter = 0;
int retryLimit = 1; //as per need

@Override
public boolean retry(ITestResult result) {
	
	if(counter &lt; retryLimit)
	{
		counter++;
		return true;
	}
	
	return false;
   }

 }

Now, add this in testng.xml file

&lt;listeners&gt;
  &lt;listener class-name=&quot;package.RetryFailedTestCases&quot;/&gt;
&lt;/listeners&gt;

this is simple and helpful when we run as suite.. alternative we can mentioned listener class on top of each class instead of testng.xml file

i am not sure why explicitly mentioned enabled = true ? even if not @Test execute right? if you dont want to execute then mention with false.

huangapple
  • 本文由 发表于 2020年9月1日 12:46:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63681543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定