java 8-Gradle测试任务testReport任务-Gradle 2.3/Java7或Java8的不推荐属性



我正在尝试将我的项目从java7更改为java8。因此,我对我用于gradle 1.6的现有gradle extra.commons-myp.gradle脚本进行了更改。

我将compile更改为JavaCompile,因为它在2.0之后已被弃用。我在测试任务中出错,

testReportDir=file("$buildDir/reports/tests/UT")testResultsDir=file("$buildDir/test-results/UT")

请告诉我缺少什么。

所有项目{应用插件:'java'应用插件:'jacoco'tasks.withType(编译){options.debug=trueoptions.compilerArgs=["-g"]}sourceSet{主要{java{srcDir'dont_change_me'}资源{srcDir'dont_change_me'}}试验{java{srcDir'dont_change_me'}资源{srcDir'dont_change_me'}}集成测试{java{srcDir'dont_change_me'}资源{srcDir'dont_change_me'}}验收测试{java{srcDir'dont_change_me'}资源{srcDir'dont_change_me'}}}jacoco{toolVersion="0.7.2.201409121644"}试验{maxParallelForks=5forkEvery=50ignoreFailures=truetestReportDir=文件("$buildDir/reports/tests/UT")testResultsDir=文件("$buildDir/test-results/UT")}//只有在Jenkins实例的额外公共文件中才需要以下Jacobo测试部分jacoco{destinationFile=file("$buildDir/jacoco/UT/jacocoUT.exc")classDumpFile=file("$buildDir/jacoco/UT/classpathdumps")}}任务集成测试(类型:测试){//始终运行测试outputs.upToDateWhen{false}ignoreFailures=truetestClassesDir=sourceSets.integrationTest.output.classesDirclasspath=sourceSets.integrationTest.runtimeClasspathtestReportDir=文件("$buildDir/reports/tests/IT")testResultsDir=文件("$buildDir/test-results/IT")//只有在Jenkins实例的额外公共文件中才需要以下Jacobo测试部分jacoco{destinationFile=file("$buildDir/jacoco/IT/jacocoIT.exec")classDumpFile=文件("$buildDir/jacoco/IT/classpathdumps")}}任务接受测试(类型:测试){//始终运行测试outputs.upToDateWhen{false}ignoreFailures=truetestClassesDir=sourceSets.integrationTest.output.classesDirclasspath=sourceSets.integrationTest.runtimeClasspathtestReportDir=文件("$buildDir/reports/tests/AT")testResultsDir=文件("$buildDir/test-results/AT")//只有在Jenkins实例的额外公共文件中才需要以下Jacobo测试部分雅科destinationFile=file("$buildDir/jacoco/AT/jacocoAT.exc")classDumpFile=file("$buildDir/jacoco/AT/classpathdumps")}}jacocoTestReport{group="报告"description="运行测试后生成Jacobo覆盖率报告。"ignoreFailures=trueexecutionData=fileTree(目录:'build/jacoco',包括:'**/*.exec')报告{xml{enabled true//以下值是一个文件destination"${buildDir}/reports/jacoco/xml/jacoco.xml"}csv.enabled falsehtml{enabled true//以下值为文件夹destination"${buildDir}/reports/jacoco.html"}}sourceDirectories=文件('src/java')classDirectories=文件('build/classes/main')}}

对于这两个属性,您得到的错误与以下类似:

No such property: testResultDirs for class: org.gradle.api.tasks.testing.Test_Decorated

好。

在1.6级或1.10之前(我想),以下属性可用于测试任务。

testReportDir = file("$buildDir/reports/tests/UT")
testResultsDir = file("$buildDir/test-results/UT")

正如你所看到的,第一个属性创建了一个自定义报告文件夹(HTML index.HTML文件将放在其中,即我们不使用默认的build/reports/tests文件夹,而是将index.HTML/和该文件旁边的其他文件放在build/reports/stests/UT文件夹下)第二个属性创建自定义测试结果文件夹(也就是说,它现在不使用构建/测试结果文件夹来放置单元测试结果*.xml文件/文件夹,而是将所有数据放在构建/测试成果/UT文件夹中)。

其想法是:在Gradle 1.6中,Gradle在您刚刚运行时创建了测试结果(.xml等)/报告文件(.html等):Gradle clean build(作为test任务,免费运行,即build调用测试任务来运行单元测试,而无需用户显式调用测试任务)。

现在,正如您使用带有Gradle 1.6<1.9/10,一切都很好,但一旦你开始使用Java8,你可能会看到Gradle 1.6与Java8不兼容的问题(由于ASM库和其他使用Java8的编译时问题(如果有的话)),因此你从使用Gradle 1.6跳到了Gradle 2.3版本。

附言:Gradle 2.4是最新的版本,但它需要在额外的全局(init.d级别)Gradle文件中进行额外的调整。我会说现在就坚持2.3级。

现在,到您的?s-如何获取自定义测试结果和报告文件夹(创建构建/测试结果/UT和构建/报告/测试/UT文件夹)。

此时,您正在将Java8与Gradle 2.3(或Java7)一起使用。现在重要的是你有了等级2.3(而不是1.6)

那么,该改变什么以及如何发现该改变什么。

  1. 参见Gradle文档2.3:https://docs.gradle.org/2.3/userguide/java_plugin.html#sec:java_test(在这里,你必须首先看到并理解,在测试任务中,从1.6级到2.3级到底发生了什么变化,以及现在将如何生成报告)。

  2. 该文档并没有在其网站上定义所有字段(Gradle可以拥有/使用的字段)。不知道为什么,但幸运的是,我们可以找到你在测试或测试报告任务中定义的所有变量(这是一个新任务,在Gradle 1.6中不可用)

    例如:https://docs.gradle.org/2.3/dsl/org.gradle.api.tasks.testing.Test.html仅显示您可以在测试任务中设置的主要属性或变量。

    幸运的是,现在测试任务中不再有testReportDirtestResultsDir属性可用(在Gradle 2.3中)。

    看看格雷德尔能看到什么。查看或运行:渐变属性

    上面的命令将列出给定Gradle x.y版本可以看到的所有变量/属性及其值。

  3. Gradle 2.3中的新任务testReport有一个概念,即当Gradle在构建过程中运行构建/测试并生成.xml或.html等文件时,您可以在显式调用testReport任务后,使用最后的报告(html部分)共同生成html报告。当你有一个多模块项目设置,并且每个子项目/模块中都有测试时,这基本上会有所帮助。如果你运行gradle clean build testReport,它将在默认位置生成.xml和.html报告(直到你在testReport任务而不是test中指定destinationDir、testResultDirs和reportsOn属性变量)。

现在,有人会说,如果我在多模块项目设置的命令行调用gradle-clean-build-testReport,您可以在给定的位置为每个子项目/模块生成.xmls和.htmls。是的,这是正确的。如果我们不想,那么我们必须在测试任务中使用以下属性(而不是testReport任务)来禁用为每个子项目创建html报告,因为很明显,我们将在最后调用testReport任务(或使用test.finalizedBytestReport)来生成所有测试的报告(如Gradle 2.3文档中所述。

test {
...
.....
reports.html.enabled = false
...
.
}

请参见此示例:https://docs.gradle.org/2.3/userguide/java_plugin.html#sec:java_test请参阅223.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
}

如果您注意到,从一个任务到另一个任务,有些变量从1.6级变为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.我添加了另一个属性,名为:testReportDirName="tests/UT"。

testReportDirName = "tests/UTnew"

PS:在doFirst{..}节/包装器中添加testReportDirName和testResultsDirName很重要(否则,如果您对integrationTest或任何集成测试任务进行了类似的更改,那么无论何时运行gradle clean build;gradle integrationTest,您的UT文件夹都将在build/tests-results/IT文件夹中创建为build/tests results/IT/tests-results/UT文件夹(前提是Tomcat已经启动/运行),然后再次进行gradle clean构建

4.在Gradle文档中的test任务中,没有关于使用或设置上述两个变量的信息:testReportDirNametestResultsDirName。找到这些属性的唯一方法是在命令行运行:gradleproperties

好的。提示-提示-你将如何改变这一点,以便做同样的事情(为IT生成结果/报告文件(集成测试,也称为集成测试任务)或验收测试任务等。我将把它留给你来了解。

我用上面的两种方法进行了测试。。现在它成功地生成了build/test-results/UT文件夹下的.xml文件和build/reports/tests/UT文件夹下的reports/html文件。

最新更新