加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
unittest.gradle 5.15 KB
一键复制 编辑 原始数据 按行查看 历史
aaa 提交于 2020-08-15 21:50 . 20200815 commit
// jacoco插件
apply plugin: 'jacoco'
// jpa-entity-generator-enhance中生成JPA Entity的插件
apply plugin: 'entitygen'
// 单元测试配置
ext {
// 单元测试配置文件路径
testConfigFilePath = "src/test/resources/groovyConfig/unit_test_config.groovy"
// 获取测试模式的参数Key
testModeKey = "testMode"
// 获取测试模式
testMode = System.getProperty(testModeKey, "default_mode")
configSlurper = new ConfigSlurper()
configSlurper.registerConditionalBlock("test_mode", testMode)
// 当前使用的测试参数
configObject = configSlurper.parse(file(testConfigFilePath).toURI().toURL())
}
test {
doFirst {
// 设置测试范围
def testInclude = configObject.get("test_include")
if (testInclude != null) {
setIncludes(testInclude)
}
// 设置排除的测试范围
def testExclude = configObject.get("test_exclude")
if (testExclude != null) {
setExcludes(testExclude)
}
minHeapSize = "256m"
maxHeapSize = "1g"
// 指定同时执行的测试类数量,默认为1
maxParallelForks = 1
/*
指定Gradle每个测试进程执行多少个测试类,超过该值后会创建新的进程
默认值为0,即一直使用一个进程执行测试类
*/
forkEvery = 5
jvmArgs "-XX:MetaspaceSize=64m", "-XX:MaxMetaspaceSize=256m"
// 日志配置
testLogging {
// 指定需要记录日志的事件
events "PASSED", "STARTED", "FAILED", "SKIPPED"
}
}
}
processResources {
setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
if (gradleArgsContains("build", "jar", "war")) {
include 'com/**'
}
}
// 在processTestResources任务中可对配置文件内容进行替换
processTestResources {
setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
// 当Gradle执行参数包含test时,执行后续操作
if (gradleArgsContains("test")) {
if (configObject == null) {
println "Unsupported test mode: " + testMode
throw new RuntimeException("Unsupported test mode: " + testMode)
}
println "testMode: " + testMode
println "test configObject: " + configObject
//替换配置文件中的测试参数
from(sourceSets.test.resources) {
include "applicationContext_replace.xml", "base_replace.properties"
filter(org.apache.tools.ant.filters.ReplaceTokens, "tokens": configObject.toProperties())
}
}
doLast {
// 使用替换参数后的配置文件进行覆盖
file('build/resources/test/base.properties').renameTo(file('build/resources/test/base.properties.bak'))
file('build/resources/test/base_replace.properties').renameTo(file('build/resources/test/base.properties'))
file('build/resources/test/applicationContext.xml').renameTo(file('build/resources/test/applicationContext.xml.bak'))
file('build/resources/test/applicationContext_replace.xml').renameTo(file('build/resources/test/applicationContext.xml'))
}
}
jacocoTestReport {
// 当Gradle执行参数包含jacocoTestReport时,执行后续操作
if (gradleArgsContains(getName())) {
def jacocoInclude = configObject.get("jacoco_include")
def jacocoExclude = configObject.get("jacoco_exclude")
// 指定需要输出jacoco代码覆盖率报告的类范围
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree('dir': it,
'include': jacocoInclude,
'exclude': jacocoExclude)
}))
}
reports {
xml.enabled = false
csv.enabled = false
html.enabled = true
html.destination = file("${buildDir}/jacocoReport")
}
}
// jpa-entity-generator-enhance中生成JPA Entity的配置
entityGen {
configPath = 'src/test/resources/entityGenConfig/entityGenConfig.yml'
}
//显示支持的测试模式
task showTestMode {
doFirst {
ConfigObject allConfigObject = new ConfigSlurper().parse(file(testConfigFilePath).toURI().toURL()).get("test_mode")
println "Usage:"
for (Map.Entry entry : allConfigObject.entrySet()) {
println "gradlew test -D" + testModeKey + "=" + entry.getKey()
}
}
}
// 判断Gradle执行的任务是否包含指定任务
boolean gradleArgsContains(String... expectedArgs) {
StartParameter startParameter = gradle.getStartParameter()
if (startParameter.isDryRun()) {
return false
}
List<TaskExecutionRequest> taskExecutionRequestList = startParameter.getTaskRequests()
for (TaskExecutionRequest taskExecutionRequest : taskExecutionRequestList) {
List<String> args = taskExecutionRequest.getArgs()
for (String arg : args) {
for (String expectedArg : expectedArgs) {
if (arg.equals(expectedArg)) {
return true
}
}
}
}
return false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化