JUnit 5: Specify execution order for nested tests

Sebastian S :

Is it possible to execute several nested tests in between of some other tests with a fixed execution order?

E.g.

@TestInstance(Lifecycle.PER_CLASS)
@TestMethodOrder(OrderAnnotation.class)
class MyTest {

    private State state = State.ZERO;

    @Test
    @Order(1)
    public void step1() throws IOException {
        state = State.ONE;
    }

    @Order(2)  // sth like this, however this annotation isn't allowed here
    @Nested
    class WhileInStateOne {

        @Test
        public void step2a {
            Assumptions.assumeTrue(state == State.ONE);

            // test something
        }

        @Test
        public void step2b {
            Assumptions.assumeTrue(state == State.ONE);

            // test something else
        }

    }

    @Test
    @Order(3)
    public void step3() throws IOException {
        state = State.THREE;
    }

}

I know, that unit tests should in general be stateless, however in this case I can save a lot of execution time if I can reuse the state in a fixed order.

Sam Brannen :

No. Tests in nested classes are always executed after tests in the enclosing class. That cannot be changed.

Ordering of test methods only applies to methods within a single test class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95217&siteId=1