Don't start nested test cases when outer test case fails with JUnit 5

 ̄綄美尐妖づ 提交于 2019-12-06 02:47:10

UPDATE:

As of JUnit Jupiter 5.4, you can develop an extension that implements the TestWatcher and ExecutionCondition APIs to achieve this behavior.

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.

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.

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.


No, as of JUnit Jupiter 5.3, there is currently no way to achieve that with out-of-the-box solutions.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!