JUnit 5 under Gradle with multiple source sets

怎甘沉沦 提交于 2019-12-04 04:32:47

Since Gradle 4.6 the gradle Java plugin has JUnit 5 support

So once you have configured the test source sets, the setup is as simple as follows - for my test and intTest test tasks I just need to configure this to use JUnit5:

apply plugin: 'java'

test {
    useJUnitPlatform()
}

intTest {
    useJUnitPlatform()
}

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.1.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.0")

}

To refine existing answers, you need to configure each of your Test-type tasks to include useJUnitPlatform(). In your concrete example this would look like

task acceptanceTest(type: Test) {
  useJUnitPlatform()
  testClassesDirs = sourceSets.acceptanceTest.output.classesDirs
  classpath = sourceSets.acceptanceTest.runtimeClasspath
  outputs.upToDateWhen { false }
}

You can also do this centrally via

tasks.withType(Test) {
    useJUnitPlatform()
}

The JUnit 5 plugin only creates one task - junitPlatformTest, and bases it on the junitPlatform extension.

If you want to run acceptance tests, or any other type of tests in another source set independently, you'll need to create your own JavaExec task, like the plugin does.

See: https://github.com/junit-team/junit5/blob/master/junit-platform-gradle-plugin/src/main/groovy/org/junit/platform/gradle/plugin/JUnitPlatformPlugin.groovy#L66

You might want to create a task like this:

task e2eTest(
        type: JavaExec,
        description: 'Runs the e2e tests.',
        group: 'Verification'
) {
    dependsOn testClasses
    shouldRunAfter test

    classpath = sourceSets.e2e.runtimeClasspath

    main = 'org.junit.platform.console.ConsoleLauncher'
    args = ['--scan-class-path',
            sourceSets.e2e.output.getClassesDirs().asPath,
            '--reports-dir', "${buildDir}/test-results/junit-e2e"]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!