gpt4 book ai didi

java - TestNG在多线程环境下重试测试

转载 作者:行者123 更新时间:2023-11-30 07:12:21 26 4
gpt4 key购买 nike

我一直在使用 TestNG 重试机制,如下 -

public class TestRetryAnalyzer implements IRetryAnalyzer {

public static final int MAX_RETRY_COUNT = 3;
private static final AtomicInteger count = new AtomicInteger(MAX_RETRY_COUNT);

public static void resetCount() {
count.set(MAX_RETRY_COUNT);
}

public int getCount() {
return count.get();
}

private boolean isRetryAvailable() {
return (count.get() > 0);
}

@Override
public boolean retry(ITestResult result) {
boolean retry = false;
if (isRetryAvailable()) {
System.out.println("Going to retry test case: " + result.getMethod() + ", " + (((MAX_RETRY_COUNT - count.get()) + 1)) + " out of " + MAX_RETRY_COUNT);
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>

当我只运行一个测试时,重试机制运行良好(1 次通过,2 次跳过)。但是当我运行上面 testng.xml 文件中提到的 5 个测试时,它们开始失败。我是否会在没有并行运行测试的情况下遇到并发问题?我该如何摆脱它?

我正在使用 testNG 6.9.10

最佳答案

TestRetryListener 仅由 TestNG 框架调用一次,因此堆中仅存在一个 TestRetryAnalyzer 实例。实例变量“count”(AtomicInteger 类的实例)在多个测试之间共享。每次测试都不会将其重置为 3,这意味着 AtomicInteger 对象仅实例化一次,并且TestRetryAnalyzer 类使用相同的引用。

为了克服上述问题,我们必须在每次测试后将计数重置为 3。为此,您必须进行以下更改

public class SimpleTest {

private int count = 0;

@Test
public void test() {
count++;
System.out.println("Test Count ... " + count);
if (count % 4 != 0) {
Assert.fail("Injected Failure");
}
count = 0;
}

@AfterTest
public void afterTest() {
TestRetryAnalyzer.resetCount();
}

}

公共(public)类 TestRetryAnalyzer 实现 IRetryAnalyzer {

private static int MAX_RETRY_COUNT = 3;

private static AtomicInteger count = new AtomicInteger(MAX_RETRY_COUNT);

public static void resetCount() {
count.set(MAX_RETRY_COUNT);
}

private boolean isRetryAvailable() {
return (count.intValue() > 0);
}

@Override
public boolean retry(ITestResult result) {
boolean retry = false;
if (isRetryAvailable()) {
retry = true;
count.decrementAndGet();
System.out.println("Retry Analyzer count is : " + count);
}
return retry;
}

}

希望这有帮助:)

关于java - TestNG在多线程环境下重试测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39021564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com