英文:
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/Retry
class
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 {
// 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 < retryLimit)
{
counter++;
return true;
}
return false;
}
}
Now, add this in testng.xml file
<listeners>
<listener class-name="package.RetryFailedTestCases"/>
</listeners>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论