本文介绍了当 TestNG @BeforeMethod 方法驻留在超类中并且运行特定组时不会调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use a group to run a subset of tests relevant to what I am working on called "current". The problem is that if I use a superclass to do some setup in a @BeforeMethod, the method runs when I run all tests, but does not when I run with just the group "current" specified.

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

So when I run all tests, the emptyTest fails because the @BeforeMethod is called, when just running group current, the method is not called. Note: If I add @Test(groups = {"current"}) to the subclass, then it does run - however, it runs all subclasses not labelled with "current" as well, which defeats the purpose of the "current" group.

如果有更好的方法来实现这种行为,我愿意接受所有解决方案.

If there is a better way to accomplish this behavior, I am open to all solutions.

谢谢,赎金

超类:

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>

推荐答案

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

Your @BeforeMethod needs to be part of the group you are running.

如果您不想对组的值进行硬编码,并且认为您将始终希望运行此方法,则也可以使用 @BeforeMethod(alwaysRun = true)您当前正在运行的组.

You can also use @BeforeMethod(alwaysRun = true) if you don't want to hardcode the value of your group and if you think you will always want to run this method, regardless of the group you are currently running.

这篇关于当 TestNG @BeforeMethod 方法驻留在超类中并且运行特定组时不会调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:23