2011-05-04 86 views
4

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. 
+0

可能重复[标记单元测试作为预期的JUnit失败](http://stackoverflow.com/questions/4055022/mark-unit-test-as-an-expected-failure-in-junit) – 2014-01-31 15:14:45

回答

8

简短回答,据我所知,没有任何扩展可以做到这一点,并且在我看来,如果JUnit存在,它将会失败。

较长的答案,红色/绿色是一种神圣的,规避它不应该成为一种习惯。如果您不小心忘记取消规避并假设所有测试都通过了?您可以预计AssertionErrorException

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

在IDE中制作一个快捷方式应该不会太难。当然,我假设你在源代码中用注释标记测试。

在我看来你所要求的是反对JUnit的目的,但我明白它的用途。

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

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

+1

我想要做到这一点的理由:假设QA为尚未实现的功能编写集成测试。然后,如果该功能仍未实现,那么我会将测试用于xFail(预期失败),如果突然失败,则会失败(因为这意味着我应该重新访问测试并将其转换为正常的单元测试)。 – user7610 2016-07-14 13:58:43

4

@Ignore标注说不,结果麻烦。

+1

哦是的,但我想打扰一下结果。特别是,应该执行标记为@wip的测试,并且它必须失败。 – 2011-05-04 21:37:31

+0

好吧定义失败?你的意思是你期望有一个例外吗? – Wes 2011-05-04 21:39:18

+0

您在描述中描述的内容与正常测试完全相反。为什么不能在你的测试中反转所有的断言,那么你就有了预期的效果。 – zenog 2013-01-10 18:19:46