Gradle测试任务testReport任务-Gradle 2.3/Java7或Java8的不推荐属性
我正在尝试将我的项目从java7更改为java8。 因此,我对用于gradle 1.6的现有gradle extra.commons-myp.gradle脚本进行了更改 我将complie更改为JavaCompile,因为它在2.0之后被弃用。 我在测试任务中出错 testReportDir = file("$buildDir/reports/tests/UT") testResultsDir = file("$buildDir/test-results/UT") 嗯 在Gradle 1.6或1.10之前(我猜),以下属性可用于测试任务Gradle测试任务testReport任务-Gradle 2.3/Java7或Java8的不推荐属性,gradle,java-8,deprecated,init.d,gradle-2,Gradle,Java 8,Deprecated,Init.d,Gradle 2,我正在尝试将我的项目从java7更改为java8。 因此,我对用于gradle 1.6的现有gradle extra.commons-myp.gradle脚本进行了更改 我将complie更改为JavaCompile,因为它在2.0之后被弃用。 我在测试任务中出错 testReportDir = file("$buildDir/reports/tests/UT") testResultsDir = file("$buildDir/test-results/UT") 嗯 在Gradle 1
testReportDir = file("$buildDir/reports/tests/UT")
testResultsDir = file("$buildDir/test-results/UT")
如您所见,第一个创建了一个自定义报告文件夹(HTML index.HTML文件将放置在其中,即,我们不使用默认的build/reports/tests文件夹,而是将index.HTML/和其他文件放在build/reports/tests/UT文件夹下)和第二个属性创建自定义测试结果文件夹(即,不再使用build/test results文件夹来放置单元测试结果*.xml文件/文件夹,而是将所有数据立即放置在build/test results/UT文件夹中)
这个想法是:在Gradle 1.6中,Gradle正在创建测试结果(.xml etc)/报告文件(.html etc),当您刚刚运行:Gradle clean build(作为测试任务,免费运行,即build调用test task来运行单元测试,而用户不显式调用测试任务)
现在,当您在Gradle 1.6<1.9/10中使用Java7时,一切都很好,但当您开始使用Java8时,您可能会看到Gradle 1.6与Java8不兼容的问题(由于ASM库和其他使用Java8的编译时问题,如果有的话),因此您从使用Gradle 1.6跳到了Gradle 2.3版本
PS:Gradle2.4是最新版本,但需要在额外的全局(init.d级别)Gradle文件中进行额外的调整。我会说现在就坚持Gradle 2.3
现在到您的?s-如何获取自定义测试结果和报告文件夹(创建了build/test results/UT和build/reports/tests/UT文件夹)
此时,您正在将Java8与Gradle 2.3(或Java7)一起使用。重要的是现在你有了Gradle 2.3(而不是1.6)
那么,改变什么以及如何找到改变什么
test {
...
.....
reports.html.enabled = false
...
.
}
参见本例:参见章节23.13.7和示例:23.14
subprojects {
apply plugin: 'java'
// Disable the test report for the individual test task
test {
reports.html.enabled = false
}
}
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}
上面的示例显示,它将在读取多模块项目结构中所有子项目的测试结果(.xml/etc info)后创建测试报告(HTML),并在build/tests/allTests文件夹中创建结果报告
现在,我们没有多模块结构,因此必须执行以下操作:1.在extra1…init.d级别的gradle文件中添加一个新任务testReport
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports/tests/UT")
testResultDirs = fileTree("$buildDir/test-results/UT")
reportOn test
}
2.更改测试任务,即
test {
maxParallelForks = 5
forkEvery = 50
ignoreFailures = true
//The following two properties DONT work in Gradle 2.3
//testReportDir = file("$buildDir/reports/tests/UT")
//testResultsDir = file("$buildDir/test-results/UT")
//With Gradle 2.3 we now need the following line to disable HTML report creation during test run per project/sub-project as we'll take care of generating those reports by testReport task which is available in Gradle 2.3.
reports.html.enabled = false
//OK - it took some time, but finally I can change the test-results
//folder which Gradle generates and uses it to put the .xml/tests results files and etc folders.
//As there is NO way to set a property in Gradle 2.3 to change the result directory property/variable,
//the only way we can do it by adding the following line.
testResultsDirName = "test-results/UT"
//The following commented out lines are optional. Un-comment if you need to.
//testLogging.showStandardStreams = true
//onOutput { descriptor, event ->
// logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
//}
//Following Jacoco test section is required only in Jenkins instance extra common file
jacoco {
//Following two properties/variables works ONLY with 1.6 of Gradle
//destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
//classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
//Following two properties/variable works only with versions >= 1.7 version of Gradle
destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
// classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
}
//Usually one should call testReport at command line while calling gradle clean build
// i.e. gradle clean build testReport
// But using Gradle 2.3 finalizedBy feature, you can call testReport as soon as test task is run by Gradle during gradle clean build.
// PS: This will bring confusion if you have a multi-module project structure as there, you should call testReport task explicitly at command line. So, comment or uncomment acc. to your use.
finalizedBy testReport
}
如果您注意到,从一个任务到另一个任务,有一些变量从Gradle 1.6更改为Gradle 2.3,这些变量/属性的名称也有一些更改(例如:testResultsDir到testResultDirs)测试任务中不再有testReportDir属性,但现在testReport任务中有destinationDir
另外,我在测试任务完成后调用testReport任务(作为test.finalizedBy testReport),以自动调用testReport任务(而不是要求用户显式调用它)
另一种解决方法,如果您不需要进行上述更改(以获得您想要实现的目标),也不想在最后运行testReport任务来运行报告,那么您还可以执行以下操作。请仔细观察这次未注释/注释的内容
//task testReport(type: TestReport) {
// destinationDir = file("$buildDir/reports/tests/UT")
// testResultDirs = fileTree("$buildDir/test-results/UT")
// reportOn test
//}
test {
maxParallelForks = 5
forkEvery = 50
ignoreFailures = true
//The following two properties DONT work in Gradle 2.3
//testReportDir = file("$buildDir/reports/tests/UT")
//testResultsDir = file("$buildDir/test-results/UT")
//With Gradle 2.3 we now need the following line to disable HTML report creation during test run per project/sub-project as we'll take care of generating those reports by testReport task which is available in Gradle 2.3.
//This time you need to comment the following line, so that it'll actually create the reports/html file during test run.
//reports.html.enabled = false
doFirst {
//OK - it took some time, but finally I can change the test-results
//folder which Gradle generates and uses it to put the .xml/tests results files and etc folders.
//As there is NO way to set a property in Gradle 2.3 to change the result directory property/variable,
//the only way we can do it by adding the following line.
testResultsDirName = "test-results/UT"
//Add the following if reports.html.enable = false line is commented out OR it's not commented but value is set to "true".
//The following line will change the default build/reports/tests folder to build/reports/tests/UT for creating html reports for Unit tests.
testReportDirName = "tests/UT"
}
//The following commented out lines are optional. Un-comment if you need to.
//testLogging.showStandardStreams = true
//onOutput { descriptor, event ->
// logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
//}
//Following Jacoco test section is required only in Jenkins instance extra common file
jacoco {
//Following two properties/variables works ONLY with 1.6 of Gradle
//destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
//classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
//Following two properties/variable works only with versions >= 1.7 version of Gradle
destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
// classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
}
//Usually one should call testReport at command line while calling gradle clean build
// i.e. gradle clean build testReport
// But using Gradle 2.3 finalizedBy feature, you can call testReport as soon as test task is run by Gradle during gradle clean build.
// PS: This will bring confusion if you have a multi-module project structure as there, you should call testReport task explicitly at command line. So, comment or uncomment acc. to your use.
//Now we don't need to call "gradle clean build testReport" anymore.
//finalizedBy testReport
}
正如您在上述代码中注意到的:
1.现在,我的全局init.d级别的gradle文件甚至没有testReport任务。
2.我已经注释掉了这一行:
//reports.html.enabled = false
3.我添加了另一个属性c
//task testReport(type: TestReport) {
// destinationDir = file("$buildDir/reports/tests/UT")
// testResultDirs = fileTree("$buildDir/test-results/UT")
// reportOn test
//}
test {
maxParallelForks = 5
forkEvery = 50
ignoreFailures = true
//The following two properties DONT work in Gradle 2.3
//testReportDir = file("$buildDir/reports/tests/UT")
//testResultsDir = file("$buildDir/test-results/UT")
//With Gradle 2.3 we now need the following line to disable HTML report creation during test run per project/sub-project as we'll take care of generating those reports by testReport task which is available in Gradle 2.3.
//This time you need to comment the following line, so that it'll actually create the reports/html file during test run.
//reports.html.enabled = false
doFirst {
//OK - it took some time, but finally I can change the test-results
//folder which Gradle generates and uses it to put the .xml/tests results files and etc folders.
//As there is NO way to set a property in Gradle 2.3 to change the result directory property/variable,
//the only way we can do it by adding the following line.
testResultsDirName = "test-results/UT"
//Add the following if reports.html.enable = false line is commented out OR it's not commented but value is set to "true".
//The following line will change the default build/reports/tests folder to build/reports/tests/UT for creating html reports for Unit tests.
testReportDirName = "tests/UT"
}
//The following commented out lines are optional. Un-comment if you need to.
//testLogging.showStandardStreams = true
//onOutput { descriptor, event ->
// logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
//}
//Following Jacoco test section is required only in Jenkins instance extra common file
jacoco {
//Following two properties/variables works ONLY with 1.6 of Gradle
//destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
//classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
//Following two properties/variable works only with versions >= 1.7 version of Gradle
destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
// classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
}
//Usually one should call testReport at command line while calling gradle clean build
// i.e. gradle clean build testReport
// But using Gradle 2.3 finalizedBy feature, you can call testReport as soon as test task is run by Gradle during gradle clean build.
// PS: This will bring confusion if you have a multi-module project structure as there, you should call testReport task explicitly at command line. So, comment or uncomment acc. to your use.
//Now we don't need to call "gradle clean build testReport" anymore.
//finalizedBy testReport
}
//reports.html.enabled = false
testReportDirName = "tests/UTnew"