在 JUnit4 中将单元测试标记为预期失败

发布于 2024-11-05 06:57:01 字数 844 浏览 3 评论 0原文

JUnit4 是否有扩展允许将某些测试标记为“预期失败”?

我想用一些标签来标记当前正在开发的功能的测试,例如@wip。对于这些测试,我想确保它们失败。

我的接受标准:

Scenario: A successful test tagged @wip is recorded as failure
    Given a successful test marked @wip
    When the test is executed
    Then the test is recorded as failure.

Scenario: A failing test tagged @wip is recorded as fine
    Given a failing test tagged @wip
    When the test is executed
    Then the test is recorded as fine.

Scenario: A successful test not tagged @wip is recorded as fine
    Given a successful test not tagged @wip
    When the test is executed
    Then the test is recorded as successful.

Scenario: A failing test not tagged with @wip is recorded as failure
    Given a failing test not tagged with @wip
    When the test is executed
    Then the test is recorded as failure.

Is there an extension for JUnit4 which allows for marking some tests as "expected to fail"?

I would like to mark the test for current features under development with some tag, for instance @wip. For these tests I would like to ensure that they are failing.

My acceptance criteria:

Scenario: A successful test tagged @wip is recorded as failure
    Given a successful test marked @wip
    When the test is executed
    Then the test is recorded as failure.

Scenario: A failing test tagged @wip is recorded as fine
    Given a failing test tagged @wip
    When the test is executed
    Then the test is recorded as fine.

Scenario: A successful test not tagged @wip is recorded as fine
    Given a successful test not tagged @wip
    When the test is executed
    Then the test is recorded as successful.

Scenario: A failing test not tagged with @wip is recorded as failure
    Given a failing test not tagged with @wip
    When the test is executed
    Then the test is recorded as failure.

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

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

发布评论

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

评论(2

各自安好 2024-11-12 06:57:01

简短回答,据我所知,没有任何扩展可以做到这一点,并且在我看来,如果 JUnit 存在的话,它将违背 JUnit 的全部目的。

更长的答案,红/绿是神圣的,规避它不应该成为一种习惯。如果您不小心忘记删除规避措施并假设所有测试都通过了怎么办?

您可以使其期望出现 AssertionErrorException

@wip
@Test(expected=AssertionError.class)
public void wipTest() {
   fail("work in progress");
}

在 IDE 中为此创建一个快捷方式应该不会太难。当然,我假设您在源代码中使用注释标记测试。

在我看来,你所问的问题违背了 JUnit 的目的,但我确实理解它的用途。

另一种方法是使用 WIP 注释实现 WIPRunner,并以某种方式使其接受使用 WIP 注释的测试失败。

如果您正在与 BDD 框架集成,我会建议一种方法,让它运行您单独标记为 @wip 的单元测试,并在您的 BDD 方法中决定结果是否正常。

Short answer, no extension will do that as far as I know and in my opinion it would defeat the whole purpose of JUnit if it would exist.

Longer answer, red/green is kind of sacred and circumventing it shouldn't become a habit. What if you accidentally forgot to remove the circumvention and assume that all tests passed?

You could make it expect an AssertionError or Exception.

@wip
@Test(expected=AssertionError.class)
public void wipTest() {
   fail("work in progress");
}

Making a shortcut in your IDE for that shouldn't be too hard. Of course I was assuming you tag the test with an annotation in the source code.

In my opinion what you are asking is against JUnit's purpose, but I do understand the use for it.

An alternative would be to implement a WIPRunner with the WIP annotation and somehow make it accept failures of tests with the WIP annotation.

If you are integrating with a BDD framework I would suggest a way to let it run the unit tests you marked @wip seperately and decide within your BDD methods if the result is ok.

薄情伤 2024-11-12 06:57:01

@Ignore 注释表示不要理会结果。

The @Ignore annotation says not to bother with the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文