1
我一直在用TestNG重試機制如下 -TestNG的重試測試
public class TestRetryAnalyzer implements IRetryAnalyzer {
private static int MAX_RETRY_COUNT = 3;
private AtomicInteger count = new AtomicInteger(MAX_RETRY_COUNT);
private boolean isRetryAvailable() {
return (count.intValue() > 0);
}
@Override
public boolean retry(ITestResult result) {
boolean retry = false;
if (isRetryAvailable()) {
Reporter.log("<br> Going to retry test case: " + result.getMethod() + ", " + (MAX_RETRY_COUNT - count.intValue() + 1) + " out of " + MAX_RETRY_COUNT+"</br>");
retry = true;
count.decrementAndGet();
}
return retry;
}
public class TestRetryListener implements IAnnotationTransformer {
@Override
public void transform(final ITestAnnotation annotation, final Class testClass, final Constructor testConstructor,
final Method testMethod) {
IRetryAnalyzer retryAnalyzer = annotation.getRetryAnalyzer();
if (retryAnalyzer == null) {
annotation.setRetryAnalyzer(TestRetryAnalyzer.class);
}
}
和測試 -
public class SimpleTest {
private int count = 0;
@Test
public void test() {
count++;
if (count % 3 != 0) {
Assert.fail("Injected Failure");
}
count = 0;
}
和的testng.xml文件 -
<suite name="Suite1" verbose="1">
<listeners>
<listener class-name="main.java.TestRetryListener" />
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
<test name="Sample" >
<classes>
<class name="main.java.SimpleTest" />
</classes>
</test>
<test name="Sample2" >
<classes>
<class name="main.java.SimpleTest" />
</classes>
</test>
<test name="Sample3" >
<classes>
<class name="main.java.SimpleTest" />
</classes>
</test>
<test name="Sample4" >
<classes>
<class name="main.java.SimpleTest" />
</classes>
</test>
<test name="Sample5" >
<classes>
<class name="main.java.SimpleTest" />
</classes>
</test>
Wh我只運行一次測試,然後重試機制運行良好(1次通過,2次跳過)。但是當我運行上面testng.xml文件中提到的5個測試時,它們開始失敗。如果沒有並行運行測試,我是否會遇到併發問題?我如何擺脫它?我用TestNG 6.9.10
這在非並行模式下工作良好,但是當我在'paral中運行測試時,會出現不一致的失敗lel =「tests」'模式: -/ – Tarun
也聲明'count'靜態會導致在同一類中重試的方法不止一次時重試失效 – Tarun
我終於明白我在做什麼了。我在'testng.xml'文件中使用了相同的類名,這導致'count'不被重置。當testng調用新的測試方法時,不需要手動重置'count'。我在不同的類中複製了測試方法,因此我的'testng.xml'具有不同的類名,現在重試分析器可以在並行和非並行模式下完美工作:) – Tarun