java TestNG:@BeforeClass 方法失败时会跳过所有后续的测试类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31476604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:41:04  来源:igfitidea点击:

TestNG: All subsequent Test classes are skipped when @BeforeClass method fails?

javaseleniumselenium-webdriverannotationstestng

提问by drkthng

My setup:

我的设置:

  • A TestBase classcontaining a @BeforeClass method
  • Several Test classesextending from TestBase class and also containing a @BeforeClass method
  • testNG 6.8.8
  • TestBase类含有@BeforeClass方法
  • 几个从 TestBase扩展的Test 类,还包含一个 @BeforeClass 方法
  • 测试NG 6.8.8

Why this setup?:

为什么这样设置?:

  • I need the @BeforeClass in the TestBase class to provide setup that all testclasses will need and I don't want to repeat in every Test class. For example thread-id-dependent login credentials.
  • TestBase class also instantiates the Selenium WebDriver
  • I need the @BeforeClass in the Test classes to initialize everything that all @Test methods will need to use but that only needs to (or must) be built/invoked once for all tests. This includes calls to said WebDriver instance (that's why a "normal" constructor doesn't work here)
  • 我需要 TestBase 类中的 @BeforeClass 来提供所有测试类都需要的设置,我不想在每个测试类中重复。例如,与线程 ID 相关的登录凭据。
  • TestBase 类还实例化 Selenium WebDriver
  • 我需要 Test 类中的 @BeforeClass 来初始化所有 @Test 方法将需要使用的所有内容,但只需要(或必须)为所有测试构建/调用一次。这包括对所述 WebDriver 实例的调用(这就是“普通”构造函数在这里不起作用的原因)

Here's what happens:

这是发生的事情:

When I run the tests via a testNG xml file and there is an exception within the @BeforeClassmethod of one of the Test classes, then all subsequent Test classes are skipped by TestNG.

当我通过 testNG xml 文件运行测试并且其中一个 Test 类的 @BeforeClass方法中存在异常时,TestNG 将跳过所有后续的 Test 类

Why does this happen? How to prevent it?

为什么会发生这种情况?如何预防?

When I change the annotation in the TestBase class to @BeforeSuitefor example, then all tests are run, even if there is an exception in on of the @BeforeClass methods.

例如,当我将 TestBase 类中的注释更改为@BeforeSuite 时,即使 @BeforeClass 方法中存在异常,也会运行所有测试

Example:

例子:

When you run the xml file, complete RunAllTestClasses02 class is skipped.

运行 xml 文件时,将跳过完整的 RunAllTestClasses02 类。

testNG xml file:

testNG xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<suite name = "MiscSuite">
    <test name = "MiscTest">
        <classes >
            <class name="drkthng.misc.RunAllTestClasses01" />
            <class name="drkthng.misc.RunAllTestClasses02" />
        </classes>
    </test>
</suite>

TestBase class with a @BeforeClass method:

带有 @BeforeClass 方法的 TestBase 类:

public abstract class RunAllTestClassesBase {

    @BeforeClass
    public void beforeClass() {
        // do something that all Test classes will need
    }
}

Test class that throws Exception within @BeforeClass method:

在@BeforeClass 方法中抛出异常的测试类:

public class RunAllTestClasses01 extends RunAllTestClassesBase {

    @BeforeClass
    public void beforeClass() {
        Assert.assertTrue(false);
    }

    @Test
    public void Test01() {
        Assert.assertTrue(true);
    }
}

采纳答案by niharika_neo

This was a bug in Testng. solved in 6.9.5. Please upgrade.

这是Testng中的一个错误。在 6.9.5 中解决。请升级。

回答by Dexter

Try to add @AfterClass(alwaysrun = true)or/and @AfterMethod(alwaysrun=true)as by default they are skipped if either BeforeClass or BeforeMethod are not completed.

尝试添加@AfterClass(alwaysrun = true)或/和@AfterMethod(alwaysrun=true)默认情况下,如果 BeforeClass 或 BeforeMethod 未完成,它们将被跳过。

The documentation on testNG Configuration Failures, Policy, and alwaysRun explains whether/when configuration failures cause listener methods (alwaysRunand other listeners) to be skipped, failure policies and best practices.

testNG Configuration Failures、Policy 和 alwaysRun 文档解释了配置失败是否/何时导致侦听器方法(alwaysRun和其他侦听器)被跳过、失败策略和最佳实践。