首页 文章

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

提问于
浏览
1

我正在尝试将我的项目从java7更改为java8 . 所以我更改了现用的gradle extra.commons-myp.gradle脚本,用于gradle 1.6 .

我将complie更改为JavaCompile,因为它在2.0之后被弃用 . 我在测试任务中遇到错误,

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

请告诉我我错过了什么 . . .

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')
      }
    }

您获得的错误类似于以下任一属性:

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

1 回答

  • 1

    好 .

    在Gradle 1.6或1.10之前(我猜), test 任务可以使用以下属性 .

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

    如您所见,第一个创建自定义报告文件夹(将放置HTML index.html文件,即不使用默认的build / reports / tests文件夹,我们希望将index.html /与其他文件放在旁边这个文件在build / reports / tests / UT文件夹下) and 第二个属性创建自定义测试结果文件夹(即不使用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 .

    Now ,给你的? - 如何获得自定义测试结果和报告文件夹(build / test-results / UT和build / reports / tests / UT文件夹创建) .

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

    那么,要改变什么以及如何找到改变的内容 .

    • 请参阅Gradle docs for 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仅显示您可以在测试任务中设置的主要属性或变量 .

    幸运的是,现在测试任务中没有 testReportDirtestResultsDir 属性(在Gradle 2.3中) .

    看看Gradle可以看到什么 . 查看或运行: gradle properties

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

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

    现在,有人会说,如果我在多模块项目设置中的命令行调用 gradle clean build testReport ,您可以在给定位置为每个子项目/模块生成.xmls和.htmls . 是的,'s correct. If we don't想要,然后我们必须在测试任务中使用以下属性(而不是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
    }
    

    上面的例子表明,'ll create the test reports (HTML) after reading the test results (.xml/etc info) from all the sub-projects in a multi-module project structure and it'll在build / tests / allTests文件夹中创建结果报告 .

    现在,我们没有多模块结构,因此我们必须执行以下操作:

    1.在extra1 ... init.d level gradle文件中添加新任务 testReport .

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

    2.更改 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.
         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 to testResultDir s ),那里有's no testReportDir property in test task anymore but there' s现在testReport任务中的destinationDir .

    此外,我在测试任务完成后调用testReport任务(如test.finalizedBy testReport)自动调用testReport任务(而不是要求用户显式调用它) .

    ANOTHER WORKAROUND ,如果您想要运行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任务 . 我已经注释掉了这一行:

    //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.没有关于使用或设置上述两个变量的信息:Gradle docs中 test 任务中的testReportDirName和testResultsDirName . 找到这些的唯一方法是在命令行运行: gradle properties .

    好 . 提示 - 提示 - 您将如何更改此操作(为IT(Integration Test又称为integrationTest任务)或acceptanceTest任务等生成结果/报告文件 . 我将留下您的查找结果 .

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

相关问题