I am trying to create test suites with JUnit5. After some research I was not able to conclude whether it is a supported feature or not.
The official user guide only mentions suites with regard to backwards compatibility to JUnit 4.
This is how it was done back in JUnit 4:
@RunWith(Suite.class) @SuiteClasses({Test1.class, Test2.class}) public class TestSuite { }
Does this mean, that Test Suites are considered deprecated now, or is the same concept still available under another name?
Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests. This chapter takes an example having two test classes, TestJunit1 & TestJunit2, that run together using Test Suite.
Just add the @Suite annotation of a class and start including or excluding the test classes and methods into the suite. When we want to run the suite, simply run it as a normal JUnit test class and it will execute all the included tests in the suite.
Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.
Does this mean, that Test Suites are considered deprecated now, or is the same concept still available under another name?
Bitter Suite Answer:
There is in fact support for test suites in JUnit 5, but it's almost certainly not what you're looking for. However, the JUnit Team is working on something that will likely suit your needs.
Detailed Answer:
As of JUnit 5.2, the only built-in support for suites is via the JUnitPlatform
Runner (registered via @RunWith(JUnitPlatform.class)
). That runner actually launches the JUnit Platform (a.k.a., JUnit 5 infrastructure) and allows you to execute tests against various programming models such as JUnit 4 and JUnit Jupiter. It also allows you to select various things (e.g., classes, packages, etc.) and configure include and exclude filters (e.g., for tags or test engines).
The problem with the runner is that it can not be executed directly on the JUnit Platform. It can only be executed using JUnit 4 by itself. That means that you lose the full reporting capabilities of the JUnit Platform since information is lost in translation from the JUnit Platform to JUnit 4.
In summary, you can technically use the JUnitPlatform
runner to execute a suite using JUnit 5, and the tests will execute, but the reporting and display in an IDE will be suboptimal.
On the bright side, the JUnit Team plans to provide built-in support for suites in JUnit 5 that does not suffer from the shortcomings of the JUnitPlatform
runner for JUnit 4. For details, see all issues assigned to the "suites" label on GitHub.
In addition, there is already a feature branch that you can check out which can be consumed in a Maven or Gradle build via JitPack.
Regards,
Sam (JUnit 5 core committer)
Test suits can now be run with JUnit 5 suite engine (from version 5.8.0).
// Aggregator dependency for junit-platform-suite-api and junit-platform-suite-engine implementation("org.junit.platform:junit-platform-suite:1.8.1")
Kotlin (replace ::class
with .class
for Java):
import org.junit.platform.suite.api.* @Suite @SelectClasses(MyTestClass1::class, MyTestClass2::class) @SelectPackages("...") @SelectDirectories("...") class MyTestSuite
The deprecation of JUnitPlatform runner:
The introduction of @Suite support provided by the junit-platform-suite-engine module makes the JUnitPlatform runner obsolete. See JUnit Platform Suite Engine for details.
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