Running unit or integration tests in specific order with junit platform suite

The intention of this example is not to encourage running unit or integration test in a specific in the build. Instead this is a powerful and handy way to reproduce random build failure due to test clashing with each other or lack of isolation between tests.

The fundamental idea is to temporarily create a junit platform suite and run the tests in the same order which caused the build failure and reproduce the failure in a consistent and deterministic way.

Once the failure can be reliably reproduced, the clashing test can be easily narrowed down.

In order to create this sample suite, we need the following test dependency:


group id: org.junit.platform

artifact id: junit-platform-suite

version: <<latest>>


Create a sample Junit suite and run it using your favourite IDE.

package com.harishkannarao.test;

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
// order of the test classes is very important. E.g ExampleTest runs first and SampleTest runs second
@SelectClasses({ExampleTest.class, SampleTest.class})
public class SampleJunitSuite {
}

The test can also be run using gradle and maven build tools as


Gradle:

gradle clean test --tests com.harishkannarao.test.SampleJunitSuite


Maven (as unit test with surefire plugin):

mvn clean install -DskipITs -Dtest=com.harishkannarao.restdatarabbitmq.test.SampleJunitSuite


Maven (as integration test with failsafe plugin):

mvn clean install -Dsurefire.failIfNoSpecifiedTests=false -Dtest=skip-unit-tests -Dit.test=com.harishkannarao.restdatarabbitmq.test.SampleJunitSuite

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux