Running Multiple Tests using JUnit

Table of contents
Reading Time: 2 minutes

In this blog we are going to discuss an approach to run multiple tests using JUnit and for this task we will user JUnit Platform Launcher API.

We are going to create a demo project in Maven that uses the Launcher API to discover and run tests.

Lets add some dependencies to our Maven project:

<dependencies>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.6.0</version>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.6.0</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.6.0</version>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-api</artifactId>
    <version>1.6.0</version>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.6.0</version>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.6.0</version>
</dependency>
</dependencies>

We now set up an instance of LauncherDiscoveryRequest. It uses a builder class where we must set package selectors and testing class name filters, to get all test classes that we want to run.

    String pkgName = SmokeTestRunner.class.getPackage().getName();

    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
                                               .request()
                                               .selectors(selectPackage(pkgName))
                                               .filters(includeTags("smoke") )
                                               .configurationParameter("junit.jupiter.execution.parallel.enabled", "true")
                                               .configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true").build();

In LauncherDiscoveryRequest selectors is used to define as to in which package to search the tests. Filters is used to add only those test over which our tag is used. We will create the Tags next, so as it can be easily implemented on our tests and discovery service can find and filter it. ConfigurationParameter is used to provide as to various configurations which are to be used while running the tests.

Since I was writing smoke tests I preferred creating a smoke test annotation so as when I call the discovery method, it can easily filter out the and run only the specific tests. To create custom tags, we will again use the Junit API

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Tag("smoke")
@Test
public @interface SmokeTest {
}

We then create the launcher object and pass on the request to the launcher so it can discover the requests. Then we create a listener to find out the summary and execute the request using execute method of launcher object.

Launcher launcher = LauncherFactory.create();

launcher.discover(request);
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);

We then get the summary and find out all the useful information from it.

TestExecutionSummary summary = listener.getSummary();
long testFound = summary.getTestsFoundCount();
long testFailed = summary.getTestsFailedCount();
long testPassed = summary.getTestsSucceededCount();

if ( testFailed > 0 ) {
fail("Smoketest Failed !!");

}

This complete code can be found here https://github.com/knoldus/junit-launcher-demo along with various numerous starter code can be easily found on Knoldus TechHub.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading