在机器人工作室Jacoco code覆盖率口味覆盖率、机器人、口味、工作室

2023-09-05 02:45:24 作者:對妞、吹囗哨

我一直在试图运行Jacoco测试覆盖率安静一段时间了。我试着在报道这些话题几种可能的解决方案:

I've been trying to run Jacoco test coverage for quiet some time now. I've tried several possible solutions reported in these topics:

Android测试code覆盖率JaCoCo摇篮插件

How采用Android摇篮插件,我会得到一个jacoco覆盖报告0.10.0或更高?

进出口运行测试的emulatated设备使用genymotion。 这是我加入build.gradle:

Im running the tests in a emulatated device using genymotion. Here is what i added to build.gradle:

apply plugin: 'jacoco'

android{       
    jacoco {
        version "0.7.1.201405082137"
    }        
    buildTypes{
        debug{
                    testCoverageEnabled = true
        }
    }
}

jacoco {
    toolVersion "0.7.1.201405082137"
}

要运行它,我使用像

./gradlew clean
./gradlew createFLAVOR_NAMEDebugCoverageReport

有关生成的文件/文件夹为:

The relevant generated files/folder are:

/build/intermediates/coverage-instrumented-classes
/build/intermediates/jacoco
/build/outputs/code-coverage/connected/flavors/MyFlavor/coverage.ec

不过,没有什么@构建/报告/ jacoco /测试/ HTML / index.html的或任何HTML页/ code覆盖报告@ /建设/输出。

However, there is nothing @ build/reports/jacoco/test/html/index.html or any html page/code coverage report @ /build/outputs.

我也尝试创建一个专门的任务,建立一个覆盖报告:

I've also tried to create a dedicated task to build a coverage report:

def coverageSourceDirs = [
    'src/main/java',
]

task jacocoTestReport(type: JacocoReport, dependsOn: "connectedAndroidTestFLAVOR_NAMEDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    classDirectories = fileTree(
        dir: './build/intermediates/classes/debug',
        excludes: ['**/R*.class',
                   '**/*$InjectAdapter.class',
                   '**/*$ModuleAdapter.class',
                   '**/*$ViewInjector*.class'
        ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/jacoco/connectedAndroidTestMyFlavorDebug.exec")
    // Bit hacky but fixes https://code.google.com/p/android/issues/detail?id=69174.
    // We iterate through the compiled .class tree and rename $$ to $.
    doFirst {
       new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

和 ./ gradlew干净和 ./ gradlew jacocoTestReport 。输出是与上述相同,所以,与覆盖率报告或任何其它覆盖文件没有html页面

Then ./gradlew clean and ./gradlew jacocoTestReport. The output is the same as above, so, no html page with coverage report or any other coverage file.

我目前使用的Andr​​oid V1.0.2工作室与最新版本的摇篮。 林相当新的摇篮,因此可以即时失去了一些东西基本在这里。

I'm currently using Android Studio v1.0.2 with the latest gradle version. Im fairly new to gradle, so it is possible im missing something basic here.

感谢

推荐答案

花了一整天追着这个问题,我发现了什么问题之后。相反,我已经看到了testDebug编译生成的文件的例子不是.exec文件@ $ buildDir / jacoco / testDebug.exec

After spending the whole day chasing this issue i found out what's the problem. Contrary to the examples i've seen the file generated by the testDebug build is not the .exec file @$buildDir/jacoco/testDebug.exec.

使用我的摇篮和录音室版本生成的文件是.ec @ 建立/输出/ code-报道/连接/香精/ myFlavor / coverage.ec

With my gradle and studio version the file generated is a .ec @build/outputs/code-coverage/connected/flavors/myFlavor/coverage.ec

我没有找到与此相关的任何相关信息。这可能是最近的变化,但是,通过创建自定义JacocoReport任务和改变executionData变量据此我已经解决了这个问题。 下面是我实现的:

I didn't found any relevant information related to this. It may be a recent change, however, by creating a custom JacocoReport task and changing the executionData variable accordingly i've solved the problem. Here is my implementation:

task jacocoTestReport(type: JacocoReport) {

  def coverageSourceDirs = [
        'src/main/java'
  ]

  group = "Reporting"
  description = "Generates Jacoco coverage reports"
  reports {
      xml{
          enabled = true
          destination "${buildDir}/reports/jacoco/jacoco.xml"
      }
      csv.enabled false
      html{
          enabled true
          destination "${buildDir}/jacocoHtml"
      }
  }

  classDirectories = fileTree(
          dir: 'build/intermediates/classes',
          excludes: ['**/R.class',
                     '**/R$*.class',
                     '**/BuildConfig.*',
                     '**/Manifest*.*',
                     '**/*Activity*.*',
                     '**/*Fragment*.*'
          ]
  )

  sourceDirectories = files(coverageSourceDirs)
  additionalSourceDirs = files(coverageSourceDirs)
  executionData = files('build/outputs/code-coverage/connected/flavors/smartcompanion/coverage.ec')
}