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

问题: I have the following test class: import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import static org.juni...

问题:

I have the following test class:

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() {}
    }
}

I want to have the behavior that checkInnerA() and checkInnerB() won't be executed when checkOuter() fails.

On the other side checkInnerB() should be executed when checkInnerA() fails because it is on the same level.

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.


回答1:

No, 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

  • 发表于 2019-01-11 01:14
  • 阅读 ( 217 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除