【问题标题】:TestNG up to 6.10 ignores subsequent classes in a suite if @BeforeMethod fails如果 @BeforeMethod 失败,TestNG 最高 6.10 会忽略套件中的后续类
【发布时间】:2017-08-04 21:56:48
【问题描述】:

考虑以下 TestNG 套件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "TestNG Examples" parallel = "false" thread-count = "1">
    <test name = "TestNG Examples">
        <classes>
            <class name = "com.example.A"/>
            <class name = "com.example.B"/>
        </classes>
    </test>
</suite>

由这些类组成:

package com.example;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public final class A extends AbstractTest {
    @BeforeMethod
    public void setUp() {
        throw new RuntimeException();
    }

    @Test
    public void test() {
    }
}

package com.example;

import org.testng.annotations.Test;

public final class B extends AbstractTest {
    @Test
    public void test() {
        System.out.println("B.test()");
    }
}

注意B.test()是在A.test()之后执行的,两个类都有一个共同的超类:

package com.example;

import org.testng.annotations.AfterMethod;

public abstract class AbstractTest {
    @AfterMethod
    public final void tearDown() {
    }
}

现在,如果 A 中的 @BeforeMethod 挂钩由于某种原因失败(如上例所示),则扩展相同超类 (AbstractTest) 的后续套件类将永远不会运行:

这是 Eclipse 的屏幕截图,但我在 IntelliJ IDEATeamCity 中也观察到了相同的行为。

必须满足以下先决条件才能重现此问题:

  • 这两个测试应该有一个共同的祖先。
  • 共同祖先应该有一个@AfterMethod 钩子。
  • 正在运行的第一个测试类应该有一个失败的 @BeforeMethod 挂钩。

这是预期的行为吗? 它是否记录在任何地方?

【问题讨论】:

  • 我确认这个问题。您能在github.com/cbeust/testng/issues 上开票吗?
  • TestNG 是什么版本的?我尝试使用 6.11,但似乎无法重现该问题。 @juherr 你试过什么版本?
  • @KrishnanMahadevan 我使用 master 分支,但尚未更新。我只用套件文件重现了这个问题。
  • @juherr - 期望应该为 B 类运行测试方法和 aftermethod,这发生在我身上。请参阅此gist,其中我创建了一个完整的示例以及断言。我在 6.11 上运行它,它可以工作。
  • @KrishnanMahadevan 我按要点回答了

标签: java unit-testing testng


【解决方案1】:

检查 alwaysRun here http://testng.org/doc/documentation-main.html.

如果我们想在依赖方法失败的情况下运行方法,那么使用 alwaysRun=true

【讨论】:

  • TestNG 6.10 及以下版本需要。 TestNG 6.11 不受该问题影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多