似乎我为该任务找到了更好的决定
task myTask(type: Junit5TestRunner) 这里我们设置一个任务
Sormuras 的回答无疑给了我一个正确的方向解决方案是将大多数样板代码移动到单独的任务类中,然后从脚本中使用该任务,从而使其更具可重用性。
班上
/**
*
* 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] }
}
}
}
脚本片段
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
}
现在 junit5 测试可以用作通常的 Gradle 任务。