关于Java 8:Gradle测试任务testReport任务-Gradle 2.3 / Java7或Java8不推荐使用的属性

Gradle test task testReport task - Deprecated properties with Gradle 2.3 / Java7 or Java8

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

我将complie更改为JavaCompile,因为它在2.0之后不推荐使用。
我在测试任务中遇到错误,

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

请建议我我所缺少的。 。 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    allprojects {
       apply plugin: 'java'
       apply plugin: 'jacoco'

        tasks.withType(Compile) {
         options.debug = true
         options.compilerArgs = ["-g"]
       }

       sourceSets {
          main {
             java {
                srcDir 'dont_change_me'
             }
             resources {
                srcDir 'dont_change_me'
             }
          }
          test {
             java {
                srcDir 'dont_change_me'
             }
             resources {
                srcDir 'dont_change_me'
             }
          }
          integrationTest {
             java {
                srcDir 'dont_change_me'
             }
             resources {
                srcDir 'dont_change_me'
             }
          }
          acceptanceTest {
             java {
                srcDir 'dont_change_me'
             }
             resources {
                srcDir 'dont_change_me'
             }
          }

       }

       jacoco {
            toolVersion ="0.7.2.201409121644"
       }

       test {
         maxParallelForks = 5
         forkEvery = 50
         ignoreFailures = true

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

         }

         //Following Jacoco test section is required only in Jenkins instance extra common file
         jacoco {
            destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
            classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
         }
       }

       task integrationTest( type: Test) {
         //Always run tests
         outputs.upToDateWhen { false }
         ignoreFailures = true

         testClassesDir = sourceSets.integrationTest.output.classesDir
         classpath = sourceSets.integrationTest.runtimeClasspath

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

         //Following Jacoco test section is required only in Jenkins instance extra common file
         jacoco {
            destinationFile = file("$buildDir/jacoco/IT/jacocoIT.exec")
            classDumpFile = file("$buildDir/jacoco/IT/classpathdumps")
         }
      }

       task acceptanceTest ( type: Test) {
         //Always run tests
         outputs.upToDateWhen { false }
         ignoreFailures = true

         testClassesDir = sourceSets.integrationTest.output.classesDir
         classpath = sourceSets.integrationTest.runtimeClasspath

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

         //Following Jacoco test section is required only in Jenkins instance extra common file
         jacoco {        
           destinationFile = file("$buildDir/jacoco/AT/jacocoAT.exec")
           classDumpFile = file("$buildDir/jacoco/AT/classpathdumps")
         }
      }

      jacocoTestReport {
          group ="Reporting"
          description ="Generate Jacoco coverage reports after running tests."
          ignoreFailures = true
          executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')

          reports {
                 xml{
                     enabled true
                     //Following value is a file
                     destination"${buildDir}/reports/jacoco/xml/jacoco.xml"
                 }
                 csv.enabled false
                 html{
                     enabled true
                     //Following value is a folder
                     destination"${buildDir}/reports/jacoco/html"
                 }
          }    
          sourceDirectories = files('src/java')
          classDirectories =  files('build/classes/main')
      }
    }

对于这些属性中的任何一个,您得到的错误与以下内容相似:

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


好。

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

好的。

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

如您所见,第一个创建了一个自定义报告文件夹(将放置HTML index.html文件,即不是使用默认的build / reports / tests文件夹,我们想将index.html /和其他文件放在旁边)该文件位于build / reports / tests / UT文件夹下),第二个属性创建自定义的test-results文件夹(即,不使用build / test-results文件夹放置单元测试结果* .xml文件/文件夹,而是实际立即将所有数据放入build / test-results / UT文件夹中)。

这个想法是:在Gradle 1.6中,Gradle在您刚运行时就创建测试结果(.xml等)/报告文件(.html等):gradle clean build(作为测试任务,免费运行,即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。

好的。

现在,到您的位置-如何获取"自定义测试结果和报告"文件夹(创建的build / test-results / UT和build / reports / tests / UT文件夹)。

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

因此,要更改的内容以及如何查找要更改的内容。

  • 请参阅Gradle文档以获取2.3: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仅显示您可以在测试任务中设置的主要属性或变量。

    幸运的是,现在测试任务中(在Gradle 2.3中)不再提供testReportDir或testResultsDir属性。

    看看所有Gradle都能看到什么。查看或运行:gradle属性

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

    好的。

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

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

    1
    2
    3
    4
    5
    6
    7
     test {
     ...
     .....
     reports.html.enabled = false
     ...
     .
     }

    参见以下示例:https://docs.gradle.org/2.3/userguide/java_plugin.html#sec:java_test参见第23.13.7节和示例:23.14。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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。

    1
    2
    3
    4
    5
    task testReport(type: TestReport) {
        destinationDir = file("$buildDir/reports/tests/UT")
        testResultDirs = fileTree("$buildDir/test-results/UT")
        reportOn test
    }

    2.更改测试任务,即

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    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任务来最后运行报表,则还可以执行以下操作。仔细观察这次没有评论/评论的内容。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    //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.我已经注释掉这一行:

    1
    //reports.html.enabled = false

    3.我添加了另一个名为:testReportDirName =" tests / UT"的属性。

    1
    testReportDirName ="tests/UTnew"

    PS:在doFirst {..}节/包装中添加testReportDirName和testResultsDirName很重要(否则,如果您对IntegrationTest或任何集成测试任务进行了类似的更改,那么您的UT文件夹将在build / tests-results / IT文件夹中创建为build / tests-results / IT / tests-results / UT文件夹,每当您运行gradle clean构建; gradle integrationTest(假设您已使Tomcat启动/运行),然后再次gradle clean构建。
    4.在Gradle文档中的任何位置,都没有关于使用或设置上述两个变量的信息:测试任务中的testReportDirName和testResultsDirName。找到这些的唯一方法是通过运行:在命令行上的gradle属性。

    好。提示-提示-您将如何更改它以执行相同的操作(为IT生成结果/报告文件(集成测试又称为IntegrationTest任务)或acceptanceTest任务等。我将留给您查找。

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

    好。