问题描述
我有以下测试课程:
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class HierarchicalTest {
@Test
void checkOuter() {
assertTrue(false);
}
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class PrepBTest {
@Test
void checkInnerA() {}
@Test
void checkInnerB() {}
}
}
我希望具有checkInnerA()
和checkInnerB()
在checkOuter()
失败时不会执行的行为.
I want to have the behavior that checkInnerA()
and checkInnerB()
won't be executed when checkOuter()
fails.
另一方面,当checkInnerA()
由于处于同一级别而失败时,应执行checkInnerB()
.
On the other side checkInnerB()
should be executed when checkInnerA()
fails because it is on the same level.
是否有一种简单的解决方案(例如,具有JUnit 5扩展名)来实现此行为?我认为这通常是所希望的行为.
Is there a simple soulution (e.g. with JUnit 5 extension) to achieve this behavior?In my opinion that's often the behavior which is wished.
推荐答案
更新:
从JUnit Jupiter 5.4开始,您可以开发实现TestWatcher
和ExecutionCondition
API的扩展来实现此行为.
As of JUnit Jupiter 5.4, you can develop an extension that implements the TestWatcher
and ExecutionCondition
APIs to achieve this behavior.
在TestWatcher
API的testFailed()
方法中,您需要跟踪有故障的测试类,并且需要将此信息存储在 root ExtensionContext.Store
中.
In the testFailed()
method from the TestWatcher
API you need to track test classes that have failures, and you need to store this information in the root ExtensionContext.Store
.
在ExecutionCondition
API的evaluateExecutionCondition()
方法中,您需要确定当前类是否为@Nested
测试类(即,内部类),并检查是否包含封闭式测试全班都失败了.如果确实如此,则需要禁用当前的@Nested
测试类,否则启用.
In the evaluateExecutionCondition()
method from the ExecutionCondition
API you need to determine if the current class is a @Nested
test class (i.e., an inner class) and check if the enclosing test class had failures. If that holds true, you need to disable the current @Nested
test class and otherwise enable it.
这些是一般准则.有关工作示例,请参见 SkipOnFailuresInEnclosingClassExtension 我刚刚发布到了GitHub上的junit5-demo
存储库中.如果该示例也用@SkipOnFailuresInEnclosingClass
注释,则该示例仅跳过@Nested
测试类就更进一步了. OuterTests 类显示实际使用的注释和扩展名.
Those are the general guidelines. For a working example, please see the SkipOnFailuresInEnclosingClassExtension I just posted to my junit5-demo
repository on GitHub. That example goes one step further by only skipping @Nested
test classes if they are also annotated with @SkipOnFailuresInEnclosingClass
. The OuterTests class shows the annotation and extension in action.
不,从JUnit Jupiter 5.3开始,当前无法使用开箱即用的解决方案来实现这一目标.
No, as of JUnit Jupiter 5.3, there is currently no way to achieve that with out-of-the-box solutions.
您可能会编写一个自定义扩展,以跟踪封闭测试类中测试的成功-例如,通过实现TestExecutionExceptionHandler
.那将需要存储在ExtensionContext.Store
中.然后,该扩展程序需要实现ExecutionCondition
,以编程方式禁用嵌套测试类.
You could potentially write a custom extension that tracks the success of tests in an enclosing test class -- for example, by implementing TestExecutionExceptionHandler
. That would need to be stored in the ExtensionContext.Store
. The extension would then need to implement ExecutionCondition
to programmatically disable nested test classes.
不幸的是,跟踪当前先前执行的测试的成功"不是很简单,但是随着新的TestWatcher
扩展API的引入,这种情况应该会得到改善,该API目前计划包含在即将发布的JUnit Jupiter 5.4中: https://github.com/junit-team/junit5/issues/542
It's unfortunately not very straightforward to track the "success" of previously executed tests currently, but that should improve with the introduction of the new TestWatcher
extension API that is currently slated for inclusion in the upcoming JUnit Jupiter 5.4: https://github.com/junit-team/junit5/issues/542
这篇关于当外部测试用例因JUnit 5失败时,请勿启动嵌套测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!