Search code examples
javagradlejunitgroovydslgradle-groovy-dsl

Run (execute) JUnit 5 Suites with Gradle task


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.


Solution

  • 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/**')}