Run (execute) JUnit 5 Suites with Gradle task

76 views Asked by At

How to execute JUnit test suites with Gradle task?

P.S. I saw another threads with questions but it is not help.

Technology stack:

  1. Gradle 7.6.4
  2. Spring Boot 2.7
  3. JUnit 5
  4. JUnit Suites ('org.junit.platform:junit-platform-suite-api:1.8.1', 'org.junit.platform:junit-platform-suite-engine:1.8.1')

Configured test suite:

@Tag("parallel")
@Suite
@SelectClasses(OneTest.class)
public class OneSuite {
    @Test
    void method() {
        System.out.println("execute: " + this.getClass().getSimpleName());
    }
}

'OneSuite' configured for run test class 'OneTest.class'.

Test in suite:

public class OneTest {
    @Test
    void firstMethod() throws InterruptedException {
        System.out.println("start " + this.getClass().getSimpleName() + ". Method one. Thread:" + Thread.currentThread().getName());
        TimeUnit.MILLISECONDS.sleep(500);
        System.out.println("end " + this.getClass().getSimpleName() + ". Method one. Thread:" + Thread.currentThread().getName());
    }
}

I try gradle task:

tasks.register('parallelTests', Test) {
    group 'tests'
    description 'Parallel test execution'
    useJUnitPlatform {
        includeTags("parallel")
        testClassesDirs = sourceSets.integrationTest.output.classesDirs
        classpath = sourceSets.integrationTest.runtimeClasspath

        systemProperty("junit.jupiter.execution.parallel.enabled", true)
        systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")
    }
    ignoreFailures = true
}

Task 'parallelTests' run olny test in suite 'method()' but doesn't run 'OneTest.class'

Console after run:

|-Test Results
  |-OneSuite
    |-method

I trying use annotation (@Tag()) on test classes without suit. Test executing normal. But when I delete tag from test and set tag to suite, gradle test running only in OneSuite, method(); do not run OneTest.class.

If running with IDE Intellij idea, suite executing good.

2

There are 2 answers

0
Oleg On BEST ANSWER

The solution is use the folder location with suites but not a tag.

tasks.register('parallelTestSuites', Test) {
group 'tests'
description 'Parallel test execution'
useJUnitPlatform {
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
}
systemProperty("junit.jupiter.execution.parallel.enabled", true)
systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")
ignoreFailures = true

include('com/myProject/somePackage/suites/**')}
1
Vampire On

This has nothing to do with Gradle, just JUnit.
Add the @Tag to OneTest and it should work.
The @Tag is not inherited from the suite down to the selected classes.
The suite just says "these classes are going to be considered", but as you filter for a specific tag, then only the tests that actually have that tag are executed.
If you run from IntelliJ without Gradle delegation, you do not filter for the tag and thus both tests are executed.