本文介绍了标记为运行太长时间的JUnit测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止并标记为运行太长时间的junit测试失败(在Maven 3构建中执行).我知道这样做的三种方式:

I would like to stop and mark as failed too long running junit tests (executed within Maven 3 build). I am aware of three ways of doing it:

1)使用带有超时参数的测试注释:

1) Using Test annotation with timeout parameter:

@Test(timeout=100)
public void testWithTimeout() {
    ...
}

2)使用规则注释:

@Rule
public Timeout globalTimeout = new Timeout(100);

3)使用以下选项配置maven-surefire-plugin:

3) Configuring maven-surefire-plugin using following options:

forkedProcessTimeoutInSeconds=1
reuseForks=false

线索是1)和2)需要更改每个测试(如果您拥有数千个测试,这会很痛苦). 3)解决方案是不可接受的,因为在许多模块中,首先测试会启动测试所使用的上下文-测试性能会急剧下降.

The clue is 1) and 2) requires to change each test (it hurts when you have many thousands). 3) solution is not acceptable as within many modules first test starts the context which is used by the test - tests performance would drastically decrease.

您还有其他想法如何实现这一目标?任何棘手的解决方案(不涉及修补JUnit:))?

Do you have any other ideas how to achieve that? Any tricky solution (not involving patching JUnit :))?

推荐答案

您可以尝试编写一个扩展BlockJunit4ClassRunner的类来尝试定义自己的测试运行程序,如果测试执行超出了定义的超时,则失败.然后用@RunWith(CustomizedTestRunner.class)注释测试类您仍然需要修改类,但是可以在单个位置中指定超时值.

You can try to define your own test runner by writing a class which extends BlockJunit4ClassRunner and fail if the test execution exceeds the defined timeout.Then annotate your test classes with @RunWith(CustomizedTestRunner.class)You still need to modify the classes but the timeout value can be specified in a single location.

这篇关于标记为运行太长时间的JUnit测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:57