为什么由于假设失败而被跳过的 junit 测试不会被报告为已跳过?

发布于 2024-09-24 23:13:44 字数 741 浏览 8 评论 0原文

我使用 junit 假设来决定是否运行测试。
假设失败的测试将被忽略/跳过 junit框架。

我想知道为什么跳过的测试不报告为“跳过”?

请看一下示例:

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!");
    }
}

在 Maven 项目中运行此测试会导致:

Running AssumptionTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec

我希望将此测试报告为“已跳过”。有机会实现这一目标吗?

(junit 4.8.1;maven 2.2.1;java 1.6.0_21)

I use junit assumptions to decide whether to run a test or not.
Tests where the assumption fails are ignored/skipped by the junit framework.

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:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难遇 2024-10-01 23:13:44

您无法开箱即用地执行此操作,因为跳过测试的唯一方法是使用 @Ignore 注释。但是,我发现了一篇博客文章这可能正是您正在寻找的:

You can't do this out-of-the-box as the only way to skip tests is with the @Ignore annotation. However, I found a blog post which might just be what you are looking for:

悲喜皆因你 2024-10-01 23:13:44

虽然之前的答案本身就很好,但我想解决“为什么”问题。

问题源于测试失败/忽略的计数方式。当添加假设时,通常的计数有点扭曲(假设可以在运行过程中忽略测试)。

工具运行者也遇到了问题,导致报告混乱。请参阅此处了解 Surefire 开发人员与 JUnit 人员的对话,以及 此处查看相关 Eclipse 错误

现在这在 Maven 中不是问题,而且在较新的 Eclipse 中也不应该出现。我使用基于 eclipse.buildId = 2.9.2.201205071000-RELEASE 的 STS,但我仍然遇到这种行为。

编辑:参考类型链接不起作用。

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.

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.

EDIT: reference type links did not work.

↘人皮目录ツ 2024-10-01 23:13:44

Spring 带来了 Ignore 注解的另一种变体,它能够在运行时决定是否忽略测试:@IfProfileValue。结合用户在 @ProfileValueSourceConfiguration 中提供的 ProfileValueSource,您可以执行一些有限的技巧。

请参阅 Spring 测试文档

但这与能够执行一些测试代码并在测试方法中间决定忽略测试不同,就像您可以通过假设一样。

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.

See Spring Test Documentation.

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.

白色秋天 2024-10-01 23:13:44

在 Maven 上,解决方案非常简单,只需将其添加到 pom.xml 中的插件部分即可!

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M5</version>           
</plugin>

on maven the solution is quite simple, just add this on plugin section in your pom.xml !

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M5</version>           
</plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文