【问题标题】:Re-running complete class and not just @Test in TestNG重新运行完整的课程,而不仅仅是 TestNG 中的 @Test
【发布时间】:2019-12-03 15:44:15
【问题描述】:

我已经浏览stackoverflow 几天了,试图找到如何重新运行整个测试类,而不仅仅是@Test 步骤。许多人说 TestNG 和 IRetryAnalyzer 不支持此功能,而有些人已发布了解决方法,但实际上并不奏效。 有没有人设法做到这一点? 并且只是为了澄清其原因,以避免说不支持的答案:TestNG 不仅是开发人员的工具。这意味着软件测试人员也将其用于 e2e 测试。 E2e 测试的步骤可以依赖于前一个步骤。所以是的,重新运行整个测试类是有效的,而不是简单的@Test,这很容易通过IRetryAnalyzer 完成。

我想要实现的一个例子是:

public class DemoTest extends TestBase {

@Test(alwaysRun = true, description = "Do this")
public void testStep_1() {
    driver.navigate().to("http://www.stackoverflow.com");
    Assert.assertEquals(driver.getCurrentUrl().contains("stackoverflow)"));

}

@Test(alwaysRun = true, dependsOnMethods = "testStep_1", description = "Do that")
public void testStep_2() {
    driver.press("button");
    Assert.assertEquals(true, driver.elementIsVisible("button"));

}

@Test(alwaysRun = true, dependsOnMethods = "testStep_2", description = "Do something else")
public void testStep_3() {
   driver.press("button2");
Assert.assertEquals(true, driver.elementIsVisible("button"));

}

}

假设testStep_2 失败,我想重新运行class DemoTest 而不仅仅是testStep_2

【问题讨论】:

标签: automated-tests testng


【解决方案1】:

好的,我知道您可能想要一些可以在 @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。

【讨论】:

  • 糟糕.. 我忘记在主监听器中添加一个条件,以便仅在重试套件发生并且成功时才更新最终报告。现已添加。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多