0

I am moving tests to junit5 that are going to be run with Gradle. In a project I work with there are unit tests and some specific tests that must be run on demand (from particular Gradle tasks I suppose).

It is clear about unit tests. Gradle plugin adds a support for that. But I could not find a way to define another test task for my needs I searched in Junit5 plugin source and found out that there are no any particular class for that purpose. The Gradle plugin simply sets up a JavaExec task and then runs it.

Therefore it seem that there are no visible ways to define my own task of the built-in type like this

task myTask(type: Junit5TestRunner) Here we set up a task

Any ideas how it can be done ?

2 Answers 2

2

Define a new configuration, depend on junit-platform-console-standalone artifact and configure the console launcher to your needs. Like:

configurations { standalone } dependencies { standalone 'org.junit.platform:junit-platform-console-standalone:1.0.0-SNAPSHOT' } task downloadJUnitPlatformStandalone(type: Copy) { from configurations.standalone into "$buildDir/junit-platform-standalone" eachFile { println " (standalone) -> " + it.file.name } } task runJUnitPlatformStandalone(type: JavaExec, dependsOn: downloadJUnitPlatformStandalone) { jvmArgs '-ea' jvmArgs '-Djava.util.logging.config.file=src/test/logging.properties' classpath = fileTree(dir: "$buildDir/junit-platform-standalone", include: '*.jar') + project.sourceSets.test.runtimeClasspath main 'org.junit.platform.console.ConsoleLauncher' args += '--scan-class-path' args += '--disable-ansi-colors' args += '--details=tree' args += "--reports-dir=$project.testReportDir" } test.dependsOn runJUnitPlatformStandalone 

Source junit-platform-standalone.gradle or alternate (Jupiter-only) dependencies jupiter.gradle.

Without own configuration and download: https://discuss.gradle.org/t/junit-5-jupiter-platform-snapshot-console-launcher-task/19773/2

Sign up to request clarification or add additional context in comments.

5 Comments

Well. Thanks I will try.
What is the purpose of downloadJUnitPlatformStandalone. May I reference to local library and not to copy ?
It downloads (copies) the dependency(ies) from configured Maven repos.
The classpath = ... option is the place where you may reference local libraries and/or your compiled test classes.
You can re-use your existing (downloaded/provided) dependencies via: Without own configuration and download: discuss.gradle.org/t/…
0

it seems like I found a bit better decision for that task

task myTask(type: Junit5TestRunner) Here we set up a task

Sormuras' answer gave me certainly a right direction The solution is to move the most boilerplate code into separate task class and then use that task from script and therefore make it more reusable.

the class

/** * * Created by Vladimir Bogodkhov on 21/04/17. * @author Vladimir Bogodkhov */ class SQJUnit5 extends JavaExec { enum Details { /** * No test plan execution details are printed. */ none("none"), /** * Test plan execution details are rendered in a flat, line-by-line mode. */ flat("flat"), /** * Test plan execution details are rendered as a simple tree. */ tree("tree"), /** * Combines tree flat modes. */ verbose("verbose"); Details(String id) { this.id = Objects.requireNonNull(id); } final String id } List<String> includeTags List<String> excludeTags List<String> includeTests = ['^.*Tests?$'] List<String> excludeTests File reportsdir Details details = Details.none; List<String> scanclasspath SQJUnit5() { jvmArgs '-ea' main 'org.junit.platform.console.ConsoleLauncher' args += '--disable-ansi-colors' args += '--details=tree' args += '--details-theme=unicode' } @Override void exec() { prepare() super.exec() } private void prepare() { if (includeTags) includeTags.each { args += ['--include-tag', it] } if (excludeTags) excludeTags.each { args += ['--exclude-tag', it] } if (includeTests) includeTests.each { args += ['--include-classname', it] } if (excludeTests) excludeTests.each { args += ['--exclude-classname', it] } if (reportsdir) { if (reportsdir.exists() && !reportsdir.isDirectory()) { throw new IllegalStateException("reportsdir must be a directory. $reportsdir.absolutePath") } args += ['--reports-dir', reportsdir.absolutePath] } if (!scanclasspath) { args += ['--scan-class-path'] } else { scanclasspath.each { args += ['--scan-class-path', it] } } } } 

Script snippet

task particularTests(type: SQJUnit5, dependsOn: build) { classpath = project.sourceSets.test.runtimeClasspath + fileTree(dir: '../../libs/junit5', include: '*.jar') excludeTags = ['DebugRun']// optional param includeTests = ['^.*Check$', '^.*Probe$']// optional param details = SQJUnit5.Details.verbose // optional param reportsdir = file('build/testReportDir') // optional param } 

Now junit5 tests can be used as a usual Gradle task.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.