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

时间:2011-02-16 18:01:16

标签: java testing testng

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

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

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

谢谢, 赎金

超类:

public class TestNGSuperclass {
    @BeforeMethod
    public void failingToShowThatItIsNotRun() {
        Assert.fail();
    }
}

子类:

@Test(groups = {"current"})
public class TestNGCurrentGroup extends TestNGSuperclass {
    public void emptyTest() {}
}

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>

2 个答案:

答案 0 :(得分:28)

您的@BeforeMethod需要成为您所在群组的成员。

如果您不想对组的值进行硬编码,并且您认为自己总是希望运行此方法,也可以使用@BeforeMethod(alwaysRun = true),无论您当前正在运行哪个组。

答案 1 :(得分:5)

你试过@BeforeMethod(groups = {"current"})吗?我得出结论,TestNG组和继承并没有那么好用。

例如,如果您运行组current中的所有内容,则上述方法有效,但如果您运行其他而不是组current,则无法运行,并且基类同时用于组。

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