在 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短回答,据我所知,没有任何扩展可以做到这一点,并且在我看来,如果 JUnit 存在的话,它将违背 JUnit 的全部目的。
更长的答案,红/绿是神圣的,规避它不应该成为一种习惯。如果您不小心忘记删除规避措施并假设所有测试都通过了怎么办?
您可以使其期望出现
AssertionError
或Exception
。在 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
orException
.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 theWIP
annotation and somehow make it accept failures of tests with theWIP
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.
@Ignore 注释表示不要理会结果。
The @Ignore annotation says not to bother with the result.