当 TestNG @BeforeMethod 方法驻留在超类中并且运行特定组时,不会调用该方法
我正在尝试使用一个组来运行与我正在从事的称为“当前”的工作相关的测试子集。问题是,如果我使用超类在 @BeforeMethod 中进行一些设置,则该方法在我运行所有测试时运行,但在我仅指定“当前”组运行时则不会运行。
因此,当我运行所有测试时,emptyTest 会失败,因为调用了 @BeforeMethod,而仅运行当前组时,不会调用该方法。注意:如果我将 @Test(groups = {"current"}) 添加到子类,那么它会运行 - 但是,它也会运行所有未标记为“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>
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.
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.
Thanks,
Ransom
Superclass:
public class TestNGSuperclass {
@BeforeMethod
public void failingToShowThatItIsNotRun() {
Assert.fail();
}
}
Subclass:
@Test(groups = {"current"})
public class TestNGCurrentGroup extends TestNGSuperclass {
public void emptyTest() {}
}
TestNG Configuration:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
@BeforeMethod
需要属于您正在运行的组。如果您不想对组的值进行硬编码,并且您认为无论您属于哪个组,都始终希望运行此方法,也可以使用 @BeforeMethod(alwaysRun = true)当前正在运行。
Your
@BeforeMethod
needs to be part of the group you are running.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.您是否尝试过
@BeforeMethod(groups = {"current"})
?我得出的结论是 TestNG 组和继承并没有真正发挥作用。例如,如果您运行组
current
中的所有内容,则上述方法有效,但如果您运行组current
之外的所有内容,则上述方法无效,并且基类为用于两组。我目前正在重构所有测试类以消除子类化并改用组合。
Have you tried
@BeforeMethod(groups = {"current"})
? I've come to the conclusion that TestNG groups and inheritance don't really work that well.For example, the above works if you run everything in group
current
, but not if you run everything other than groupcurrent
and the base class is used for both groups.I'm currently refactoring all our test classes to eliminate subclassing and to make use of composition instead.
如果您使用带有继承的 testNg 组,请提供
alwaysRun = true
以便运行@BeforeMethod
,示例如下:Provide
alwaysRun = true
in order to run@BeforeMethod
if you are using testNg groups with inheritance, example given: