Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run JUnit 5 and JUnit 4 test suites in Gradle?

I have two types of tests in my code, ending with UnitTest and IntegrationTest. Of course there are some legacy JUnit 4 tests and new ones supposed to be written with JUnit 5.

What I want:

UnitTestSuite and IntegrationTestSuit classes that could be run from IDE (IntelliJ IDEA) and each of them have filters by the class name ending of tests. Also I want two different gradle tasks each to run their own set of tests (based on suits ideally, or also on the class names at least).

What I've tried:

This test suite works well from IDE, and as I understand it should run both JUnit 4 and JUnit 5 tests. However, it seems that this approach is more like a workaround and not actual suites support.

@RunWith(JUnitPlatform.class)
@IncludeClassNamePatterns({ "^.*UnitTest$" })
public class UnitTestSuite {
}

Also I created this Gradle task, but it doesn't run any tests saying to me:

WARNING: Ignoring test class using JUnitPlatform runner

test { Test t ->

    useJUnitPlatform()

    include "UnitTestSuite.class"
}

So is there a solution to run both JUnit 4 and JUnit 5 tests, filtered by name (gathered into suits) from the IDE and from the Gradle task?

like image 612
KeLsTaR Avatar asked Feb 02 '19 12:02

KeLsTaR


People also ask

Can we use JUnit 4 and 5 together?

JUnit 5 provides a way out of the box. Each one is a distinct project and using all of them allows to compile and execute JUnit 4 and JUnit 5 tests in a same project. JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5.

Is JUnit 5 backwards compatible with JUnit 4?

Please note that JUnit 5 is not backward compatible with JUnit 4, but the JUnit team created the JUnit Vintage Project to support JUnit 4 test cases on top of JUnit 5.

How do you run a parallel test in Gradle?

To run the unit tests in parallel using gradle we use the below script in every project under test task. We also maintain the single gradle. properties file. Is it possible to define test.


1 Answers

In Gradle you can configure multiple test tasks, one for JUnit 4 and one for JUnit 5.

I did exactly that in the Spring Framework build. See the testJUnitJupiter and test tasks in spring-test.gradle.

task testJUnitJupiter(type: Test) {
    description = "Runs JUnit Jupiter tests."
    useJUnitPlatform {
        includeEngines "junit-jupiter"
        excludeTags "failing-test-case"
    }
    filter {
        includeTestsMatching "org.springframework.test.context.junit.jupiter.*"
    }
    reports.junitXml.destination = file("$buildDir/test-results")
    // Java Util Logging for the JUnit Platform.
    // systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
}

test {
    description = "Runs JUnit 4 tests."
    dependsOn testJUnitJupiter, testNG
    useJUnit()
    scanForTestClasses = false
    include(["**/*Tests.class", "**/*Test.class"])
    exclude(["**/testng/**/*.*", "**/jupiter/**/*.*"])
    reports.junitXml.destination = file("$buildDir/test-results")
}

You can of course name them and configure them however you want.

like image 157
Sam Brannen Avatar answered Nov 05 '22 08:11

Sam Brannen