java 为什么由于假设失败而跳过的 junit 测试未报告为已跳过?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3814974/
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
Why is a junit test that is skipped because of an assumption failure is not reported as skipped?
提问by FrVaBe
I use junit assumptions to decide whether to run a test or not.
Tests where the assumption fails are ignored/skippedby the junit framework.
我使用 junit 假设来决定是否运行测试。junit 框架忽略/跳过
假设失败的测试。
I wonder why skipped tests are not reported as 'skipped'?
我想知道为什么跳过的测试没有报告为“跳过”?
Please have a look at the example:
请看一下示例:
import static org.junit.Assert.fail;
import org.junit.Assume;
import org.junit.Test;
public class AssumptionTest {
@Test
public void skipAssumptionFailureTest() {
Assume.assumeTrue("foo".equals("bar"));
fail("This should not be reached!");
}
}
Running this test in a maven project results in:
在 Maven 项目中运行此测试会导致:
Running AssumptionTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec
I would prefer to have this test reported as 'skipped'. Is there any chance to achieve this?
我更愿意将此测试报告为“跳过”。有没有机会实现这一目标?
(junit 4.8.1; maven 2.2.1; java 1.6.0_21)
(junit 4.8.1;maven 2.2.1;java 1.6.0_21)
采纳答案by dogbane
回答by LAFK says Reinstate Monica
While previous answers are good on their own, I'd like to address the "WHY" question.
虽然以前的答案本身很好,但我想解决“为什么”的问题。
The problem stemmed from the fact how tests failed / ignored were counted. When assumptions were added, usual counts were kinda skewered (assumption can IGNORE the tests half-way through it's run).
问题源于测试失败/忽略的计算方式。当添加假设时,通常的计数有点偏斜(假设可以在运行中途忽略测试)。
Tools runners had problems with that as well, causing messy reporting. See here for Surefire dev conversation with JUnit people, and here for related Eclipse bug.
工具运行器也有问题,导致报告混乱。请在此处查看与 JUnit 人员的 Surefire 开发对话,在此处查看相关的 Eclipse 错误。
Right now this ain't a problem in Maven, and it should not be in newer Eclipses. I use STS based on eclipse.buildId = 2.9.2.201205071000-RELEASE
and I still experience this behaviour.
现在这在 Maven 中不是问题,它不应该在较新的 Eclipses 中。我使用基于的 STSeclipse.buildId = 2.9.2.201205071000-RELEASE
并且我仍然遇到这种行为。
EDIT: reference type links did not work.
编辑:引用类型链接不起作用。
回答by Christian Semrau
Spring brings another variant of an Ignore annotation, one that is able to decide at runtime whether to ignore a test: @IfProfileValue
. In combination with a user supplied ProfileValueSource
in a @ProfileValueSourceConfiguration
, you can do some limited tricks.
春天带来了忽略注释,一个是能够在运行时决定是否忽略测试的另一种变体:@IfProfileValue
。在与提供的用户组合ProfileValueSource
在一@ProfileValueSourceConfiguration
,你可以做一些有限的技巧。
See Spring Test Documentation.
请参阅Spring 测试文档。
But it is not the same as being able to perform some test code and in the middle of the test method decide to ignore the test, as you can with assumptions.
但这与能够执行一些测试代码并在测试方法的中间决定忽略测试不同,就像您可以假设一样。