重试逻辑 - 如果一个测试失败,则重试整个班级 - selenium [英] Retry Logic - retry whole class if one tests fails - selenium
问题描述
以下是用于实现重试逻辑的类
Following are the classes used to implement retry logic
TestRetry 类:
TestRetry Class:
public class TestRetry implements IRetryAnalyzer {
int counter=0;
int retryLimit=2;
@Override
public boolean retry(ITestResult result) {
if (counter < retryLimit) {
TestReporter.logStep("Retrying Test " +result.getName()+" for number of times: "+(counter+1));
counter++;
return true;
}
return false;
}
RetryListener 类:
RetryListener Class:
public class RetryListener implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
// TODO Auto-generated method stub
IRetryAnalyzer retry = annotation.getRetryAnalyzer();
if (retry == null) {
annotation.setRetryAnalyzer(TestRetry.class);
}
}}
样本测试:
@Listeners(RetryListener.class)
public class SampleTest {
@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context) {
for (ITestNGMethod method : context.getAllTestMethods()) {
method.setRetryAnalyzer(new TestRetry());
}
}
@Test(priority=0)
public void firsttest() {
System.out.println();
TestReporter.assertEquals("Test", "Test", "pass");
}
@Test(priority=1, dependsOnMethods="firsttest")
public void secondtest() {
TestReporter.assertEquals("Test", "Test1", "fail");
}
@Test(priority=2,dependsOnMethods="secondtest")
public void thirdtest() {
TestReporter.assertEquals("Test", "Test", "pass");
}
}
当我执行上述测试时,输出如下firsttest 被执行并通过secondtest 依赖于 firsttest 并被执行,它失败了 - 重试了 3 次并再次失败跳过了第三次测试,因为它取决于第二次测试.
When I execute the above test, following is the output firsttest gets executed and passes secondtest depends on firsttest and gets executed, its failed - Retried 3 times and failed again thirdtest skipped because it depends on secondtest.
输出达到预期.
问题:由于测试是相关的.如果其中一个测试失败,我想从一开始就执行整个课程.有办法吗?
Question: Since the tests are dependent. If one of the tests fails, I want to execute the whole class from first. is there a way to do it?
示例:如果 secondtest 失败,我想再次执行整个类 SampleTest.
Examples: If secondtest fails, I want to execute the whole class SampleTest again.
谢谢!
推荐答案
目前无法实现您的要求.
There's currently no way of achieving what you are asking for.
TestNG 只会重试失败的测试,但不会上升执行阶梯以找出所有上游依赖项并尝试运行它们(您的问题是此通用用例的一个非常具体的变体).
TestNG will only retry a failed test, but will not go up the execution ladder to find out all the upstream dependencies and try running them as well (Your ask is a very specific variant of this generic use case).
仔细想想,一个依赖测试之所以被执行,只是因为它的上游依赖(它依赖的方法)已经成功执行了.因此,如果当前测试失败,为什么需要重新执行已经满足的上游依赖项?它的反直觉.
If you come to think of it, a dependent test is being executed only because its upstream dependencies (methods on which it depends on) have been executed successfully. So if there's a failure in the current test, why would one need to re-execute the already satisfied upstream dependencies? Its counter intuitive.
对于您拥有的用例,您应该仅在 @Test
方法中构建整个逻辑,其中您负责处理重试以及整个链的调用再一次,如果有失败.
For what you have as a use-case, you should be merely building the entire logic within a @Test
method, wherein you take care of handling the retries and also the invocation of the entire chain once again, if there were failures.
下面的示例应该说明
public class SampleTest {
@Test (retryAnalyzer = TestRetry.class)
public void orchestrateTest() {
firsttest();
secondtest();
thirdtest();
}
public void firsttest() {
System.out.println();
TestReporter.assertEquals("Test", "Test", "pass");
}
public void secondtest() {
TestReporter.assertEquals("Test", "Test1", "fail");
}
public void thirdtest() {
TestReporter.assertEquals("Test", "Test", "pass");
}
}
TestNG 不支持您在问题中寻找的用例.
TestNG does not support the use case that you are looking for in your question.
附带说明,您不能通过 @Listeners
注释(在此接口的 javadoc 中明确调用)连接到 IAnnotationTransformer
侦听器.它只能通过套件 xml 中的 <listeners>
标记(或)通过在 META-INF\services\org.testng.ITestNGListener
文件(在 Java 中称为 Service Provider Interface 方法)
On a side note, you cannot wire in a IAnnotationTransformer
listener via an @Listeners
annotation (this is explicitly called out in the javadocs of this interface). It can only be wired in via the <listeners>
tag in your suite xml (or) by referring to it in the META-INF\services\org.testng.ITestNGListener
file (its called the Service Provider Interface approach in Java)
这篇关于重试逻辑 - 如果一个测试失败,则重试整个班级 - selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!