Saturday, 28 August 2021

Junit5: Test Suite: Group multiple test classes

You can combine multiple test classes into a suite.

 

Dependency to be added

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>${junit.platform.version}</version>
    <scope>test</scope>
</dependency>

Example

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy", "com.sample.app.arithmetic"})
public class SuiteDemo1 {

}


‘SuiteDemo1’ class run the tests methods located in the packages "com.sample.app.dummy", and "com.sample.app.arithmetic".

 

Find the below working application.

 

ArithmeticTest.java

package com.sample.app.arithmetic;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class ArithmeticTest {

    private static int sum(int a, int b) {
        return a + b;
    }

    @Test
    public void test1() {
        assertEquals(5, sum(2, 3));
    }

}


DummyTest.java

package com.sample.app.dummy;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class DummyTest {

    @Test
    public void test1() {
        assertTrue(true);
    }
}


SuiteDemo1.java

package com.sample.app.suite;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy", "com.sample.app.arithmetic"})
public class SuiteDemo1 {

}


Run SuiteDemo1.java, you will see the test methods executed in junit window.





'org.junit.platform.suite.api' package provides below annotations to support suite specific tasks.

 

Annotation

Description

ExcludeClassNamePatterns

@ExcludeClassNamePatterns specifies regular expressions that are used to match against fully qualified class names when running a test suite on the JUnit Platform.

ExcludeEngines

@ExcludeEngines specifies the IDs of TestEngines to be excluded when running a test suite on the JUnit Platform.

ExcludePackages

@ExcludePackages specifies the packages to be excluded when running a test suite on the JUnit Platform.

ExcludeTags

@ExcludeTags specifies the tags or tag expressions to be excluded when running a test suite on the JUnit Platform.

IncludeClassNamePatterns

@IncludeClassNamePatterns specifies regular expressions that are used to match against fully qualified class names when running a test suite on the JUnit Platform.

IncludeEngines

@IncludeEngines specifies the IDs of TestEngines to be included when running a test suite on the JUnit Platform.

IncludePackages

@IncludePackages specifies the packages to be included when running a test suite on the JUnit Platform.

IncludeTags

@IncludeTags specifies the tags or tag expressions to be included when running a test suite on the JUnit Platform.

SelectClasses  

@SelectClasses specifies the classes to select when running a test suite on the JUnit Platform.

SelectPackages

@SelectPackages specifies the names of packages to select when running a test suite on the JUnit Platform.

SuiteDisplayName 

@SuiteDisplayName is used to declare a custom display name for the annotated test class that is executed as a test suite on the JUnit Platform.

UseTechnicalNames

@UseTechnicalNames specifies that technical names should be used instead of display names when running a test suite on the JUnit Platform.





 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment