JUnit 5——全局超时?
JUnit 5 -- global timeout?
在 JUnit 5 中,为所有测试强制执行全局超时的最佳方法是什么?
我看到 JUnit 4 有一个 @Rule
功能,但它已在 JUnit 5 中删除。我不希望每次测试都必须复制粘贴 assertTimeoutPreemptively
。
在 JUnit 5.5 版之前,无法在 JUnit Jupiter 中为测试强制执行全局抢占式超时。此功能已作为实验性功能添加到 JUnit 5.5。
要查看有关此功能的讨论,请参阅以下问题:https://github.com/junit-team/junit5/issues/80
Junit 5.5 支持 "global timeout"。看看 documentation of corresponding property knobs.
例如,尝试打开文件 src/test/resources/junit-platform.properties
并粘贴到那里:
junit.jupiter.execution.timeout.default=42 ms
您提到了 JUnit 4 Timeout
@Rule
, which allows specifying timeouts on a per-test class basis. JUnit 5.5 has a direct equivalent in the @Timeout
注释。这可以以与 JUnit 4 规则相同的方式应用于 class 级别,在这种情况下,它适用于 class 及其 @Nested
classes 中的所有测试.它还可以应用于单个 @Test
或生命周期(@BeforeAll
、@BeforeEach
、@AfterEach
或 @AfterAll
)方法。
The @Timeout
annotation allows one to declare that a test, test factory, test template, or lifecycle method should fail if its execution time exceeds a given duration. The time unit for the duration defaults to seconds but is configurable.
[...]
To apply the same timeout to all test methods within a test class and all of its @Nested
classes, you can declare the @Timeout
annotation at the class level. It will then be applied to all test, test factory, and test template methods within that class and its @Nested
classes unless overridden by a @Timeout
annotation on a specific method or @Nested
class. Please note that @Timeout
annotations declared at the class level are not applied to lifecycle methods.
在 JUnit 5 中,为所有测试强制执行全局超时的最佳方法是什么?
我看到 JUnit 4 有一个 @Rule
功能,但它已在 JUnit 5 中删除。我不希望每次测试都必须复制粘贴 assertTimeoutPreemptively
。
在 JUnit 5.5 版之前,无法在 JUnit Jupiter 中为测试强制执行全局抢占式超时。此功能已作为实验性功能添加到 JUnit 5.5。
要查看有关此功能的讨论,请参阅以下问题:https://github.com/junit-team/junit5/issues/80
Junit 5.5 支持 "global timeout"。看看 documentation of corresponding property knobs.
例如,尝试打开文件 src/test/resources/junit-platform.properties
并粘贴到那里:
junit.jupiter.execution.timeout.default=42 ms
您提到了 JUnit 4 Timeout
@Rule
, which allows specifying timeouts on a per-test class basis. JUnit 5.5 has a direct equivalent in the @Timeout
注释。这可以以与 JUnit 4 规则相同的方式应用于 class 级别,在这种情况下,它适用于 class 及其 @Nested
classes 中的所有测试.它还可以应用于单个 @Test
或生命周期(@BeforeAll
、@BeforeEach
、@AfterEach
或 @AfterAll
)方法。
The
@Timeout
annotation allows one to declare that a test, test factory, test template, or lifecycle method should fail if its execution time exceeds a given duration. The time unit for the duration defaults to seconds but is configurable.[...]
To apply the same timeout to all test methods within a test class and all of its
@Nested
classes, you can declare the@Timeout
annotation at the class level. It will then be applied to all test, test factory, and test template methods within that class and its@Nested
classes unless overridden by a@Timeout
annotation on a specific method or@Nested
class. Please note that@Timeout
annotations declared at the class level are not applied to lifecycle methods.