java 当 TestNG @BeforeMethod 方法驻留在超类中并且运行特定组时不会调用它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5020260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TestNG @BeforeMethod method not called when it resides in superclass and a specific group is run
提问by Ransom Briggs
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.
我正在尝试使用一个组来运行与我正在处理的称为“当前”的测试相关的测试子集。问题是,如果我使用超类在 @BeforeMethod 中进行一些设置,则该方法会在我运行所有测试时运行,但当我仅使用指定的“当前”组运行时不会运行。
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.
因此,当我运行所有测试时,emptyTest 失败,因为调用了 @BeforeMethod,当只运行 group current 时,不会调用该方法。注意:如果我将 @Test(groups = {"current"}) 添加到子类,那么它会运行 - 但是,它也会运行所有未标有“current”的子类,这违背了“current”组的目的.
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:
测试NG配置:
<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>
回答by Cedric Beust
Your @BeforeMethod
needs to be part of the group you are running.
您@BeforeMethod
需要成为您正在运行的团队的一部分。
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(alwaysRun = true)
,如果你不希望你的硬编码组的值,如果你认为你会一直要运行这个方法,无论您当前正在运行的组。
回答by Brian Agnew
Have you tried @BeforeMethod(groups = {"current"})
? I've come to the conclusion that TestNG groups and inheritance don't really work that well.
你试过@BeforeMethod(groups = {"current"})
吗?我得出的结论是,TestNG 组和继承实际上并没有那么好用。
For example, the above works if you run everything in group current
, but not if you run everything otherthan group current
and the base class is used for both groups.
例如,上面的作品,如果你运行组的一切current
,但如果你运行一切其他比组current
和基类是用于这两个群体。
I'm currently refactoring all our test classes to eliminate subclassing and to make use of composition instead.
我目前正在重构我们所有的测试类以消除子类化并改用组合。