好的,我知道您可能想要一些可以在 @BeforeClass 中指定的简单属性或类似的东西,但我们可能需要等待实现。至少我也找不到。
以下内容非常丑陋,但我认为它可以完成这项工作,至少在小范围内,还有待观察它在更复杂的场景中的表现。也许有更多的时间,这可以改进成更好的东西。
好的,所以我创建了一个类似于你的测试类:
public class RetryTest extends TestConfig {
public class RetryTest extends TestConfig {
Assertion assertion = new Assertion();
@Test( enabled = true,
groups = { "retryTest" },
retryAnalyzer = TestRetry.class,
ignoreMissingDependencies = false)
public void testStep_1() {
}
@Test( enabled = true,
groups = { "retryTest" },
retryAnalyzer = TestRetry.class,
dependsOnMethods = "testStep_1",
ignoreMissingDependencies = false)
public void testStep_2() {
if (fail) assertion.fail("This will fail the first time and not the second.");
}
@Test( enabled = true,
groups = { "retryTest" },
retryAnalyzer = TestRetry.class,
dependsOnMethods = "testStep_2",
ignoreMissingDependencies = false)
public void testStep_3() {
}
@Test( enabled = true)
public void testStep_4() {
assertion.fail("This should leave a failure in the end.");
}
}
我在超类中有Listener,以防我想将其扩展到其他类,但您也可以在测试类中设置监听器。
@Listeners(TestListener.class)
public class TestConfig {
protected static boolean retrySuccessful = false;
protected static boolean fail = true;
}
上述 4 种方法中有 3 种具有 RetryAnalyzer。我留下了 testStep_4 没有它,以确保我接下来要做的事情不会干扰其余的执行。说RetryAnalyzer实际上不会重试(注意该方法返回false),但它会执行以下操作:
public class TestRetry implements IRetryAnalyzer {
public static TestNG retryTestNG = null;
@Override
public boolean retry(ITestResult result) {
Class[] classes = {CreateBookingTest.class};
TestNG retryTestNG = new TestNG();
retryTestNG.setDefaultTestName("RETRY TEST");
retryTestNG.setTestClasses(classes);
retryTestNG.setGroups("retryTest");
retryTestNG.addListener(new RetryAnnotationTransformer());
retryTestNG.addListener(new TestListenerRetry());
retryTestNG.run();
return false;
}
}
这将在您的执行中创建一个执行。它不会弄乱报告,一旦完成,它将继续您的主要执行。但它会“重试”该组中的方法。
是的,我知道,我知道。这意味着您将在一个永恒的循环中永远执行您的测试套件。这就是RetryAnnotationTransformer 的原因。在其中,我们将从这些测试的第二次执行中删除 RetryAnalyzer:
public class RetryAnnotationTransformer extends TestConfig implements IAnnotationTransformer {
@SuppressWarnings("rawtypes")
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
fail = false; // This is just for debugging. Will make testStep_2 pass in the second run.
annotation.setRetryAnalyzer(null);
}
}
现在我们遇到了最后一个问题。我们最初的测试套件对那里的“重试”执行一无所知。这是它变得非常丑陋的地方。我们需要告诉记者刚刚发生的事情。这是我鼓励你改进的部分。我没有时间做一些更好的事情,但如果可以的话,我会在某个时候编辑它。
首先,我们需要知道 retryTestNG 执行是否成功。可能有一百万种方法可以更好地做到这一点,但现在这可行。我为重试执行设置了一个监听器。您可以在上面的TestRetry 中看到它,它由以下内容组成:
public class TestListenerRetry extends TestConfig implements ITestListener {
(...)
@Override
public void onFinish(ITestContext context) {
if (context.getFailedTests().size()==0 && context.getSkippedTests().size()==0) {
successful = true;
}
}
}
现在是主套件的侦听器,您在上面的超类TestConfig 中看到的侦听器将查看运行是否发生以及运行是否顺利并更新报告:
public class TestListener extends TestConfig implements ITestListener , ISuiteListener {
(...)
@Override
public void onFinish(ISuite suite) {
if (TestRetry.retryTestNG != null) {
for (ITestNGMethod iTestNGMethod : suite.getMethodsByGroups().get("retryTest")) {
Collection<ISuiteResult> iSuiteResultList = suite.getResults().values();
for (ISuiteResult iSuiteResult : iSuiteResultList) {
ITestContext iTestContext = iSuiteResult.getTestContext();
List<ITestResult> unsuccessfulMethods = new ArrayList<ITestResult>();
for (ITestResult iTestResult : iTestContext.getFailedTests().getAllResults()) {
if (iTestResult.getMethod().equals(iTestNGMethod)) {
iTestContext.getFailedTests().removeResult(iTestResult);
unsuccessfulMethods.add(iTestResult);
}
}
for (ITestResult iTestResult : iTestContext.getSkippedTests().getAllResults()) {
if (iTestResult.getMethod().equals(iTestNGMethod)) {
iTestContext.getSkippedTests().removeResult(iTestResult);
unsuccessfulMethods.add(iTestResult);
}
}
for (ITestResult iTestResult : unsuccessfulMethods) {
iTestResult.setStatus(1);
iTestContext.getPassedTests().addResult(iTestResult, iTestResult.getMethod());
}
}
}
}
}
}
报告现在应该显示 3 个测试通过(因为它们被重试)和一个失败,因为它不是其他 3 个测试的一部分:
我知道这不是您想要的,但我会帮助它为您服务,直到他们将功能添加到 TestNG。