将单元测试标记为JUnit4中的预期故障

时间:2011-05-04 21:15:30

标签: unit-testing junit junit4

是否有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.

2 个答案:

答案 0 :(得分:8)

简短回答,据我所知,没有任何扩展会这样做,在我看来,如果它存在,它将会击败JUnit的整个目的。

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

您可以期待AssertionErrorException

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

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

在我看来,你所要求的是反对JUnit的目的,但我确实理解它的用途。

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

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

答案 1 :(得分:4)

@Ignore注释表示不打扰结果。