当前位置: 首页 > 知识库问答 >
问题:

Gradle test task testReport task-不推荐使用Gradle 2.3/Java7或Java8的属性

戈宏义
2023-03-14

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


    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个答案

龙佐
2023-03-14

好吧。

在Gradle1.6或直到1.10之前(我猜),以下属性可用于测试任务。

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

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

这个想法是:在Gradle1.6中,Gradle正在创建测试结果(.xml等)/报告文件(.html等),当您刚刚运行:Gradle clean build(作为测试任务,免费运行,即build调用测试任务来运行单元测试,而不需要用户显式调用测试任务)。

现在,当您使用Gradle1.6<1.9/10的Java7时,事情是好的,但是当您开始使用Java8时,您可能会看到Gradle1.6与Java8不兼容的问题(由于ASM库和使用Java8时出现的其他编译时问题),因此您从使用Gradle1.6跳到了Gradle2.3版本。

PS:Gradle2.4是最新的一个,但是它需要在额外的全局(init.d级别)Gradle文件中进行额外的调整。我会说坚持2.3级。

现在,到您的?s-如何获得自定义测试结果和报表文件夹(创建的build/test-results/ut和build/reports/tests/ut文件夹)。

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

那么,改什么,怎么找改什么。

对于ex:https://docs.gradle.org/2.3/dsl/org.gradle.api.tasks.testing.test.html只显示您可以在测试任务中设置的主要属性或变量。

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

看看所有Gradle都能看到什么。请参阅或运行:gradle properties

 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中添加新任务testReport...init.d级分级文件。

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.
     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
   }

如果您注意到,有一些变量从Gradle1.6到Gradle2.3,从一个任务到另一个任务,这些变量/属性的名称也做了一些更改(例如:TestResultDir到testResultDirs),并且test task中不再有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级分级文件甚至没有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.在gradle文档中的任何地方都没有关于在测试任务中使用或

好的。提示-提示-你将如何改变它来做同样的事情(为它生成结果/报告文件(Integration Test,又名integrationTest任务)或acceptanceTest任务等),我将留给你去了解。

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

 类似资料:
  • 当尝试运行gradle build时,我在Android Studio 2.2中收到以下警告: 警告:android。dexOptions。不推荐使用incremental属性,它对生成过程没有影响。 内置。gradle文件在这里我声明了dexOptions 如果不推荐使用,那么该属性的替代选项是什么。

  • 我试图将我的项目升级到一些最新版本,但由于项目中的一些依赖项尚未更新,我无法获得空安全性。 我已经求助于sdk版本2.10,同时我更新了所有核心包,解决了所有突破性的更改。一些不推荐的更改(如FlatButton到TextButton,尚未解决)。 我在构建解决方案时遇到了几个问题,在咨询了一些旧的Stack帖子后,我拼凑了gradle文件更新,升级到Android Studio等... 但是,当

  • 我已经使用了最新版本的video_player:^0.10.5+1),但显示以下警告。 注意:某些输入文件使用或重写不推荐的API。 注意:使用-xlint重新编译:有关详细信息。 注意:注意:详细信息请使用-xlint:deprecation重新编译。

  • 我在最新的Android Studio上创建了一个全新的Android项目。我做的第一件事是将`realm'库添加到项目中,方法是将以下内容添加到gradle文件中: 如果我尝试编译,我会得到以下错误: Origine2:C:\users\usmaan.gradle\caches\caches\modules-2\files-2.1\io.realm\realm-android\0.80.3\79

  • 问题内容: 我收到此警告,但是该程序仍然可以正常运行。 MySQL代码向我显示了一条PHP消息: 不推荐使用:mysql_connect():不推荐使用mysql扩展,以后将被删除:在第2行的C:\ xampp \ htdocs \ task \ media \ new \ connect.inc.php中使用mysqli或PDO代替 我的页面是 这是什么意思,我该如何消除该消息? 问题答案: 有

  • 问题内容: 我正在尝试使用和进行单元测试。 当我不包含注释时,测试将失败。但 不推荐使用MockitoJUnitRunner类型 我正在使用Mockito 2.6.9。我应该怎么做? 问题答案: 现在确实已弃用,应该改为使用。如您所见,仅软件包名称已更改,该类的简单名称仍为 。 摘录自javadoc : 移至,该课程将在Mockito 3中删除