I'm organizing my tests using JUnit 5's @Suite and @SelectClasses, but need to control the execution sequence of the test classes. According to the documentation, @SelectClasses doesn't guarantee execution order, but I have a specific sequence requirement for my test pipeline.
Here's my current suite setup:
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectClasses({
Test1.class, // Should run FIRST
Suite1.class, // Should run after Test1 but before Test2
Suite2.class, // Should run after Test1 but before Test2
Suite3.class, // Should run after Test1 but before Test2
Test2.class // Should run LAST
})
public class SuiteA { }
The subordinate suites look like:
@Suite
@SelectClasses({TestXxx.class, SuiteXxx.class})
public class Suite1 { }
My test dependencies require that:
Test1 executes first (initial setup and validation)
Suite1, Suite2, Suite3 execute in between (main test batches)
Test2 executes last (cleanup and final verification)
What I've tried/researched:
I know about @TestMethodOrder for ordering methods within a class, but I need class-level ordering across the suite
I've looked at @Suite documentation but didn't find explicit ordering controls
I considered separate suites, but need this specific sequence in a single execution
Question:
What is the standard JUnit 5 approach to enforcing test class execution order within a suite? If declarative ordering isn't supported, what are the recommended patterns or workarounds to achieve this test sequence? I'm using JUnit 5.8+/Jupiter with the junit-platform-suite engine.
Please provide a concrete solution - I already know @SelectClasses doesn't order tests. I need a working implementation.