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

时间:2015-06-23 06:35:21

标签: 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")

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


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

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

SELECT event FROM table WHERE (date <= CURDATE()) ORDER BY date DESC LIMIT 3;

1 个答案:

答案 0 :(得分:1)

行。

在Gradle 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 / tests / UT 文件夹下的此文件旁边)第二个属性创建自定义测试结果文件夹(即不使用构建/测试结果放置单元测试结果* .xml文件/文件夹的文件夹,它实际上将所有数据放在 build / test-results / UT 文件夹中。

这个想法是:在Gradle 1.6中,当您刚刚运行时,Gradle正在创建测试结果(.xml等)/报告文件(.html等): gradle clean build (作为测试任务,免费运行,即构建调用测试任务以运行单元测试,而无需用户明确调用测试任务)。

现在,正如您使用Java7与Gradle 1.6&lt; 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)。

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

  1. 请参阅Gradle docs for 2.3:https://docs.gradle.org/2.3/userguide/java_plugin.html#sec:java_test (在这里你必须首先看到并理解,在测试任务中从Gradle 1.6到2.3的确切变化以及如何生成报告)。

  2. 该文档没有在其网站上定义的所有字段(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 x.y版本可以看到的值。

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

  4. 现在,有人会说,如果我在多模块项目设置中的命令行调用 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 info)后创建测试报告(HTML)并且&# 39; ll在 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 to 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任务。 我已经注释掉了这一行:

    //reports.html.enabled = false
    

    3。我添加了另一个属性:testReportDirName =&#34; tests / UT&#34;。

    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文档中的任何位置使用或设置上述两个变量的信息: testReportDirName testResultsDirName 测试任务中。您可以找到这些的唯一方法是在命令行运行: gradle properties

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

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