[英]@AfterMethod being ignored when rerunning test in TestNG
I'm trying to build logic into my test suite so that if a test fails, it automatically gets rerun. 我正在尝试在测试套件中构建逻辑,以便如果测试失败,它将自动重新运行。 When my test fails, it gets rerun as expected, but my test suite contains an @AfterMethod which seems to be getting ignored when the test is getting rerun.
当我的测试失败时,它将按预期方式重新运行,但是我的测试套件包含一个@AfterMethod,当重新运行测试时,该@AfterMethod似乎将被忽略。
I've followed the solution given here: 我遵循了此处给出的解决方案:
https://www.softwaretestingmaterial.com/run-failed-test-cases-using-testng/ https://www.softwaretestingmaterial.com/run-failed-test-cases-using-testng/
So I created the 2 classes: 因此,我创建了2个类:
package softwareTestingMaterial;
import org.testng.IRetryAnalyzer; import org.testng.ITestResult;
public class RetryFailedTestCases implements IRetryAnalyzer {
private int retryCnt = 0;
private int maxRetryCnt = 2;
public boolean retry(ITestResult result) {
if (retryCnt < maxRetryCnt) {
System.out.println("Retrying " + result.getName() + " again and the count is " + (retryCnt+1));
retryCnt++;
return true;
}
return false;
}
}
And 和
package softwareTestingMaterial;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;
public class RetryListenerClass implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
if (retry == null) {
testannotation.setRetryAnalyzer(RetryFailedTestCases.class);
}
}
}
My XML file for executing the tests looks as follows: 我用于执行测试的XML文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite 1">
<listeners>
<listener class-name="org.testng.reporters.EmailableReporter2" />
<listener class-name="CustomUtils.RetryListenerClass" />
</listeners>
<test name="Test 1">
<classes>
<class name="WebTests.CheckoutTestWeb"/>
</classes>
</test>
</suite>
When @Test throws exceptions, @AfterMethod may not run. 当@Test引发异常时,@ AfterMethod可能不会运行。 To ensure @AfterMethod to be executed, add alwaysRun attribute which will look like @AfterMethod(alwaysRun = true)
为了确保执行@AfterMethod,请添加alwaysRun属性,该属性将类似于@AfterMethod(alwaysRun = true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.