Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are test suites considered deprecated in JUnit5?

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?

like image 310
AscendingEagle Avatar asked May 28 '18 11:05

AscendingEagle


People also ask

Is Suite test supported by JUnit?

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.

Which of the following is the correct way of including Suite classes in junit5?

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.

What is difference between junit4 and junit5?

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.


2 Answers

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)

like image 149
Sam Brannen Avatar answered Sep 22 '22 11:09

Sam Brannen


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.

like image 42
Mahozad Avatar answered Sep 20 '22 11:09

Mahozad