简体   繁体   English

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

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

My setup:我的设置:

  • A TestBase class containing a @BeforeClass method包含 @BeforeClass 方法的TestBase 类
  • Several Test classes extending from TestBase class and also containing a @BeforeClass method几个从 TestBase扩展的Test 类,还包含一个 @BeforeClass 方法
  • testNG 6.8.8测试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.我需要 TestBase 类中的 @BeforeClass 来提供所有测试类都需要的设置,我不想在每个测试类中重复。 For example thread-id-dependent login credentials.例如,与线程 ID 相关的登录凭据。
  • TestBase class also instantiates the Selenium WebDriver TestBase 类还实例化 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.我需要 Test 类中的 @BeforeClass 来初始化所有 @Test 方法将需要使用的所有内容,但只需要(或必须)为所有测试构建/调用一次。 This includes calls to said WebDriver instance (that's why a "normal" constructor doesn't work here)这包括对所述 WebDriver 实例的调用(这就是“普通”构造函数在这里不起作用的原因)

Here's what happens:这是发生的事情:

When I run the tests via a testNG xml file and there is an exception within the @BeforeClass method 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 @BeforeSuite for 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);
    }
}

This was a bug in Testng. 这是Testng中的一个错误。 solved in 6.9.5. 在6.9.5中解决。 Please upgrade. 请升级。

I am new to autmation.I have been given existing framework but when i am trying to run that existing code it is giving me below output我是 autmation 的新手。我已经获得了现有框架,但是当我尝试运行该现有代码时,它给了我以下输出

All Test Suite Total tests run: 0, Passes: 0, Failures: 0, Skips: 0所有测试套件 运行的测试总数:0,通过:0,失败:0,跳过:0

Where i am going wrong and what i am missing please help我哪里出错了,我错过了什么,请帮忙

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或@AfterMethod(alwaysrun=true) ,则会跳过它们。

The documentation on testNG Configuration Failures, Policy, and alwaysRun explains whether/when configuration failures cause listener methods ( alwaysRun and other listeners) to be skipped, failure policies and best practices. 有关testNG配置失败,策略和alwaysRun的文档说明了配置失败是否/何时导致跳过侦听器方法( alwaysRun和其他侦听器),失败策略和最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM