java 什么会导致 JUnit 忽略 @Ignore 注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6217935/
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
What can cause JUnit to disregard @Ignore annotations?
提问by Pops
I just used MyEclipse to automatically generate some JUnit test cases. One of the generated methods looks like this:
我只是用 MyEclipse 自动生成了一些 JUnit 测试用例。生成的方法之一如下所示:
@Ignore("Ignored") @Test
public void testCreateRevision()
{
fail("Not yet implemented"); // TODO
}
I added the @Ignore
annotation manually. However, when I run the test, JUnit lists that method, and others like it, under "failures," rather than ignoring them (related: What's the difference between failure and error in JUnit?). And it displays the "Not yet implemented" message instead of the "Ignored" message. Clearly, fail()
must be getting called, and therefore, the @Ignore
assertion is not working.
我@Ignore
手动添加了注释。但是,当我运行测试时,JUnit 会在“失败”下列出该方法以及其他类似方法,而不是忽略它们(相关:JUnit 中的失败和错误有什么区别?)。它显示“尚未实现”消息而不是“已忽略”消息。显然,fail()
必须被调用,因此,@Ignore
断言不起作用。
What's going on here? Is there a setting I need to enable for this to work?
这里发生了什么?是否需要启用设置才能使其正常工作?
EDIT :
Things I have considered/tried so far:
编辑:
到目前为止我已经考虑/尝试过的事情:
- I am using JUnit 4, so it's not a version problem.
- I am importing
org.junit.Ignore
, so it's not a case of the wrongIgnore
being used. - I have tried using
@Ignore
alone,@Ignore @Test
and@Ignore("message") @Test
; all fail.
- 我使用的是 JUnit 4,所以这不是版本问题。
- 我正在导入
org.junit.Ignore
,所以这不是Ignore
使用错误的情况。 - 我试过
@Ignore
单独使用,@Ignore @Test
并且@Ignore("message") @Test
;都失败了。
EDIT 2 :
I created the tests with MyEclipse, via New > Other; Java > JUnit > JUnit Test Case; New JUnit 4 test
, and the library in my build path is JUnit 4. I'm building with ant and actually running the case with MyEclipse.
编辑 2:
我使用 MyEclipse 创建了测试,通过New > Other; Java > JUnit > JUnit Test Case; New JUnit 4 test
,我的构建路径中的库是 JUnit 4。我正在使用 ant 构建并实际使用 MyEclipse 运行这个案例。
回答by Tomasz Nurkiewicz
Make sure you are importing the right
@Ignore
. To be sure use@org.junit.Ignore
explicitly.Double check if your test is being executed by JUnit 4, not 3. The easiest way to do this is to either change the test name so it is not prefixed by
test
(now it shouldn't be executed at all and JUnit 4 does not need this prefix anyway) or examine your test case inheritance hierarchy: it shouldn'textend directly or indirectly fromjunit.framework.TestCase
(Junit 3 requirement).
确保您正在导入正确的
@Ignore
. 确保@org.junit.Ignore
明确使用。仔细检查您的测试是否由 JUnit 4 执行,而不是 3。最简单的方法是更改测试名称,使其
test
不带有前缀(现在根本不应该执行并且 JUnit 4 不需要这个前缀无论如何)或检查您的测试用例继承层次结构:它不应该直接或间接地从junit.framework.TestCase
(Junit 3 要求)扩展。
回答by compiledweird
I had this problem also even though JUnit 3 was not on my classpath. I believe that compatibility mode on Junit 4 picks up on the 'test' prefix in your testname and thus operates as JUnit 3 would rather than picking up the @Ignore. The solution is to rename your test.
即使 JUnit 3 不在我的类路径中,我也遇到了这个问题。我相信 Junit 4 上的兼容模式会选择测试名称中的“test”前缀,因此像 JUnit 3 一样运行,而不是选择 @Ignore。解决方案是重命名您的测试。
回答by kay - SE is evil
Are you sure the test classes were recompiled?
您确定重新编译了测试类吗?
It's a quite common problem, that the recompilation fails because there was typo somewhere in the sources (like a missing semicolon), and the IDE does not tell you that compiling failed.
这是一个非常常见的问题,重新编译失败是因为源代码中的某处有拼写错误(例如缺少分号),并且 IDE 不会告诉您编译失败。
Try deleting the target/test-classes
folder.
尝试删除target/test-classes
文件夹。
回答by Mateusz Dyszkiewicz
In my case I found my IDE was executing the test disregarding the @Ignore
annotation. When I ran mvn install
(or any other maven phase) the test was skipped and that's what I was actually aiming for (see attached ilustration).
就我而言,我发现我的 IDE 正在执行测试而忽略了@Ignore
注释。当我运行mvn install
(或任何其他 maven 阶段)时,测试被跳过,这就是我的实际目标(请参阅随附的 ilustration)。