TestNG @BeforeMethod方法在驻留在超类中并且运行特定组时未调用

Ran*_*ggs 16 java testing testng

我正在尝试使用一个组来运行与我正在处理的"当前"相关的测试子集.问题是,如果我使用超类在@BeforeMethod中进行某些设置,则该方法在我运行所有测试时运行,但在我仅使用指定的组"current"运行时则不运行.

因此,当我运行所有测试时,emptyTest失败,因为调用了@BeforeMethod,当只运行组current时,不调用该方法.注意:如果我将@Test(groups = {"current"})添加到子类,那么它确实运行 - 但是,它运行所有未标记为"current"的子类,这违背了"当前"组的目的.

如果有更好的方法来实现此行为,我对所有解决方案持开放态度.

谢谢,赎金

超类:

public class TestNGSuperclass {
    @BeforeMethod
    public void failingToShowThatItIsNotRun() {
        Assert.fail();
    }
}
Run Code Online (Sandbox Code Playgroud)

子类:

@Test(groups = {"current"})
public class TestNGCurrentGroup extends TestNGSuperclass {
    public void emptyTest() {}
}
Run Code Online (Sandbox Code Playgroud)

TestNG配置:

<test name="current">
    <groups>
        <run>
            <include name="current"/>
        </run>
    </groups>
    <packages>
        <package name="uiowa.wf.test.*"/>
    </packages>
</test>
<test name="all-tests">
    <packages>
       <package name="uiowa.wf.test.*"/>
    </packages>
</test>
Run Code Online (Sandbox Code Playgroud)

Ced*_*ust 32

@BeforeMethod需要成为正在运行的组的一部分.

您也可以使用@BeforeMethod(alwaysRun = true),如果你不希望你的硬编码组的值,如果你认为你会一直要运行这个方法,无论您当前正在运行的组.


Bri*_*new 5

你试过了@BeforeMethod(groups = {"current"})吗?我得出的结论是,TestNG组和继承不能很好地工作。

例如,上面的作品,如果你运行组的一切current,但如果你运行一切其他比组current和基类是用于这两个群体。

我目前正在重构所有测试类,以消除子类并改为使用组合。