如果我@忽略JUnit4中的测试类,@BeforeClass是否仍然运行? [英] If I @Ignore a test class in JUnit4, does @BeforeClass still run?
问题描述
快速背景:我几天来一直在寻找Maven/Surefire测试运行问题,并且将范围缩小到少数测试嫌疑人.我看到的行为是疯狂.我从mvn clean test
开始:运行250个测试,跳过0个.现在,我将可疑测试移至src/test/java
并重试:运行146个测试,跳过0个! Maven的输出提供 no 提示,即使使用-X
标志,也没有在运行其他测试.
Quick background: I've been hunting down a Maven / Surefire test-running problem for days now, and I've narrowed it down to a small number suspect of tests. The behavior I'm seeing is insane. I start with mvn clean test
: 250 tests run, 0 skipped. Now, I move the suspect test into src/test/java
and try again: 146 tests run, 0 skipped! The output of Maven gives no clue that other tests aren't being run, even with the -X
flag.
这使我想到了一个问题:我称测试为可疑"的原因是整个类都用@Ignore装饰,因此我可以想象在我的测试源中包含它应该是 no 管理虚拟Zookeeper服务器.以前是导致行为不灵通的原因,这就是为什么我们要进行@Ignored测试的原因.
That brings me to my question: the reason I call the test 'suspect' is that the whole class is decorated with @Ignore, so I would imagine that including it in my test sources should have no effect at all. Then it occurred to me -- those classes have @BeforeClass/@AfterClass methods that manage a dummy Zookeeper server. It's resulted in wonky behavior before, which is why we have the tests @Ignored.
如果JUnit在运行之前/之后的代码,但是忽略了测试,我不知道会发生什么(但是可能会非常糟糕).这是发生了吗?这应该发生吗?如果是这样,当包含@BeforeClass/@AfterClass时,我应该说供参考,这是一个应该应该工作但需要修复的测试"?同样令人感兴趣的是:这对Surefire/Maven到底有什么用,它会导致无关的测试 从地球表面掉下 ?
If JUnit is running the before/after code but ignoring the tests, I have no idea what might happen (but it'd probably be super bad). Is this happening? Is this supposed to happen? If so, how am I supposed to say "for reference, here's a test that should work but needs fixing" when it includes @BeforeClass / @AfterClass? Also of substantial interest: what the hell is this doing to Surefire / Maven, that it causes unrelated tests to fall off the face of the Earth?
推荐答案
如果您使用@Ignore批注进行测试,则对于@BeforeClass& @AfterClass可以运行,无论是否所有测试都被忽略.
If you have a test with the @Ignore annotation, then it is normal behaviour for the @BeforeClass & @AfterClass to get run, whether or not all of the tests are @Ignored.
但是,如果类具有@Ignore批注,则@BeforeClass& @AfterClass不要运行.
If, however, the Class has an @Ignore annotation, then the @BeforeClass & @AfterClass don't get run.
对于Maven,如果您不想在特定类中运行任何测试,则必须在surefire或failsafe中忽略它们.将此添加到Maven配置(请参见 Maven Surefire插件 )
For maven, if you don't want to run any tests in a particular class, then you have to ignore them in surefire or failsafe. Add this to the maven configuration (see Maven Surefire Plugin)
<excludes>
<exclude>**/FoobarTest.class</exclude>
</excludes>
这篇关于如果我@忽略JUnit4中的测试类,@BeforeClass是否仍然运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!