好的。
在 Gradle 1.6 或 1.10 之前(我猜),以下属性可用于测试任务。
testReportDir = file("$buildDir/reports/tests/UT")
testResultsDir = file("$buildDir/test-results/UT")
如您所见,第一个创建了一个自定义报告文件夹(将放置 HTML index.html 文件,即,我们希望将 index.html/ 与其他文件放在旁边,而不是使用默认的 build/reports/tests 文件夹此文件在build/reports/tests/UT文件夹下),第二个属性创建自定义测试结果文件夹(即,而不是使用 build/test-results 文件夹来放置单元测试结果 *.xml 文件/文件夹,它实际上会现在将所有这些数据放在build/test-results/UT文件夹中)。
这个想法是:在 Gradle 1.6 中,Gradle 正在创建测试结果(.xml 等)/报告文件(.html 等),而您刚刚运行:gradle clean build(作为测试任务,免费运行,即构建调用测试任务运行没有用户显式调用测试任务的单元测试)。
现在,当您使用 Java7 和 Gradle 1.6 < 1.9/10 时,一切都很好,但是一旦您开始使用 Java8,您可能会看到 Gradle 1.6 与 Java8 不兼容的问题(由于 ASM 库和其他编译时问题使用 Java8(如果有),因此您从使用 Gradle 1.6 跳到了 Gradle 2.3 版本。
PS:Gradle 2.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)。
因此,要更改什么以及如何找到要更改的内容。
请参阅 2.3 的 Gradle 文档:https ://docs.gradle.org/2.3/userguide/java_plugin.html#sec:java_test
(在这里您必须首先查看并理解,在测试任务中从 Gradle 1.6 到 2.3 到底发生了什么变化和现在将如何生成报告)。
该文档没有在其站点上定义所有字段(Gradle 可以拥有/使用的)。不知道为什么,但幸运的是,我们可以找出您在 test 或 testReport 任务中定义的所有变量(这是一项新任务,在 Gradle 1.6 中不可用)。
例如:https ://docs.gradle.org/2.3/dsl/org.gradle.api.tasks.testing.Test.html仅显示您可以在测试任务中设置的主要属性或变量。
幸运的是,现在测试任务中不再提供testReportDir或testResultsDir属性(在 Gradle 2.3 中)。
查看所有 Gradle 可以看到的内容。查看或运行:gradle 属性
上面的命令将列出所有变量/属性以及给定 Gradle xy 版本可以看到的值。
新任务testReport或 Gradle 2.3 有一个概念,现在当 Gradle 在构建期间运行 build/tests 并生成 .xml 或 .html 等文件时,您可以使用最后的报告(html 部分)集体生成 HTML 报告在显式调用testReport任务之后。当您有一个多模块项目设置并且每个子项目/模块中都有测试时,这基本上会有所帮助。如果您运行 gradle clean build testReport ,它将在默认位置生成 .xml 和 .html 报告(直到您在testReport任务而不是test任务中指定 destinationDir、testResultDirs 和 reportsOn 属性变量)。
现在,有人会说,如果我在多模块项目设置的命令行中调用gradle clean build testReport,您可以在给定位置为每个子项目/模块生成 .xmls 和 .htmls。对,那是正确的。如果我们不想要,那么我们必须在测试任务(而不是 testReport 任务)中使用以下属性来禁用为每个子项目创建 html 报告,显然,我们将在最后调用 testReport 任务(或使用test.finalizedBy testReport)为所有测试生成报告(如 Gradle 2.3 文档中所述。
test {
...
.....
reports.html.enabled = false
...
.
}
请参阅此示例:https ://docs.gradle.org/2.3/userguide/java_plugin.html#sec:java_test 请参阅第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 信息)后创建测试报告(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,并且这些变量/属性的名称也发生了一些变化(例如: testResult s Dir 到 testResultDir s)并且没有 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. 我添加了另一个名为:testReportDirName = "tests/UT" 的属性。
testReportDirName = "tests/UTnew"
PS:在 doFirst { .. } 部分/包装器中添加 testReportDirName 和 testResultsDirName 很重要(否则,如果您对 integrationTest 或任何集成测试任务进行类似更改,那么您的 UT 文件夹将在 build/tests-results/IT 文件夹中创建为build/tests-results/IT/tests-results/UT 文件夹,每当您运行 gradle clean build 时;gradle integrationTest(前提是您已启动/运行 Tomcat)并再次 gradle clean build。4.
没有关于使用或设置的信息以上两个变量: testReportDirName和testResultsDirName在Gradle 文档中的任何地方的测试任务中。找到它们的唯一方法是在命令行运行: gradle properties 。
好的。提示 - 提示 - 你将如何改变它来做同样的事情(为 IT 生成结果/报告文件(集成测试又名集成测试任务)或验收测试任务等。我将把它留给你找出来。
我用上述两种方法进行了测试。现在它成功地在 build/test-results/UT 文件夹下生成 .xml 文件和在 build/reports/tests/UT 文件夹下生成报告/html 文件。