Single JUnit5 test execution with Gradle

2019-07-17 03:25发布

问题:

I tried to run only one test class from Gradle. I used these commands:

gradle -Dtest.single=Junit5Test test
gradle test --tests ru.tests.runner.Junit5Test

But it runs all of the project's tests. I need to run only one test class. Here is my class:

public class Junit5Test {

      public static void main(String[] args) {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
            .selectors(
                selectPackage("ru.tests.api")
            )
            .build();

        Launcher launcher = LauncherFactory.create();
        launcher.execute(request, new TestExecutionListener());
      }
    } 

It works perfect by right-click run. I use

  • IntellijIDEA 2017.1.5,
  • Gradle,
  • junitPlatformVersion = '1.0.0-M6',
  • junitJupiterVersion = '5.0.0-M6'

回答1:

For starters, your Junit5Test class is not a "test class".

Rather, it is a stand-alone application (i.e., a Java class with a main() method).

Thus, attempting to execute it as a "test" with Gradle will never work.

If you would like to run JUnit Jupiter tests (or any other tests that run on the JUnit Platform) with Gradle, you should use the JUnit Platform Gradle Plugin (that's a link to the appropriate section of the JUnit 5 User Guide).



回答2:

I resolved it by replacing

gradle -Dtest.single=Junit5Test test

with

gradle -DjunitPlatformTest.single=Junit5Test test