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 particul
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.
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