如何为JUnit测试用例运行启用全局超时? [英] How to enable a global timeout for JUnit testcase runs?

查看:108
本文介绍了如何为JUnit测试用例运行启用全局超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题建议使用

This question suggests to use the timeout parameter of the @Test annotation to have JUnit forcefully stop tests after that timeout period.

但是到目前为止,我们已经进行了5000项单元测试,并且我们希望建立一项政策,要求开发人员从不发布需要10秒钟以上才能完成的测试.该政策可能会说瞄准时间少于10秒",但是我们要确保在30秒后停止 any 测试.(数字仅是示例,其想法是为大多数用例定义足够好"的东西,但也可以确保事情不会永远"运行)

But we have like 5000 unit tests so far, and we want to establish a policy that asks developers to never release tests that need more than 10 seconds to complete. The policy would probably say "aim for < 10 seconds", but then we would like to ensure that any test is stopped after say 30 seconds. (the numbers are just examples, the idea is to define something that is "good enough" for most use cases, but that also makes sure things dont run "forever" )

现在,我想知道是否有一种方法可以启用而无需进入每个测试用例并添加该批注参数.

Now I am wondering if there is a way to enable such behavior without turning into each test case and adding that annotation parameter.

现有的

The existing question doesn't help either: I am looking for one change to enable this, not a one change per test class solution. One central, global switch. Not one per file or method.

推荐答案

尽管JUnit Jupiter(即JUnit 5中引入的编程和扩展模型)还没有内置 ="https://github.com/junit-team/junit5/issues/80" rel ="noreferrer">对全局超时的支持,您仍然可以自行实现全局超时的支持.

Although JUnit Jupiter (i.e., the programming and extension model introduced in JUnit 5) does not yet have built-in support for global timeouts, you can still implement global timeout support on your own.

唯一的问题是超时扩展当前无法抢先终止测试执行.换句话说,JUnit Jupiter中的超时扩展当前仅可以计时测试的执行时间,如果执行时间太长(例如,在等待测试结束之后),则抛出异常,如果测试挂起).

The only catch is that a timeout extension cannot currently abort test execution preemptively. In other words, a timeout extension in JUnit Jupiter can currently only time the execution of tests and then throw an exception if the execution took too long (i.e., after waiting for the test to end, which may potentially never happen if the test hangs).

无论如何,如果要实现与JUnit Jupiter一起使用的非抢先全局超时扩展,这就是您需要做的.

In any case, if you want to implement a non-preemptive global timeout extension for use with JUnit Jupiter, here's what you need to do.

  1. 查看 TimingExtension 示例以获取灵感.您将需要类似的代码,但是如果 duration 超过配置的 timeout ,则将引发异常.如何配置全局超时取决于您:硬编码,从JVM系统属性中查找值,从自定义注释中查找值等.
  2. 使用Java的 ServiceLoader 机制注册全局超时扩展.有关详细信息,请参见自动扩展注册.
  3. >
  1. Look at the TimingExtension example in the JUnit 5 User Guide for inspiration. You'll need code similar to that, but you'll want to throw an exception if the duration exceeds a configured timeout. How you configure your global timeout is up to you: hard code it, look up the value from a JVM system property, look up the value from a custom annotation etc.
  2. Register your global timeout extension using Java's ServiceLoader mechanism. See Automatic Extension Registration for details.

测试愉快!

这篇关于如何为JUnit测试用例运行启用全局超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆