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.
All nested test classes must be annotated with the @Nested annotation. The depth of the test class hierarchy is not limited in any way. A nested test class can contain test methods, one @BeforeEach method, and one @AfterEach method. By default, we cannot add the @BeforeAll and @AfterAll methods to a nested test class.
Once parallel test execution property is enabled, the JUnit Jupiter engine will execute tests in parallel according to the provided configuration with declared synchronization mechanisms.
JUnit execution sequence Execution sequence with the same annotation is in order of appearance in the file. Methods annotated with @BeforeClass and @AfterClass – those public static void methods which do some setup/teardown just once before and after all tests have started/passed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With