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.