好像我找到了一个好一点决定了该任务
任务myTask(类型: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测试可以作为一个平常摇篮任务。
好吧。谢谢,我会尝试。 –
downloadJUnitPlatformStandalone的用途是什么。我可以参考当地图书馆,不要复制? –
它从配置的Maven仓库下载(复制)依赖关系。 – Sormuras