简体   繁体   English

如何使用 Android Studio 获得代码覆盖率?

[英]How to get code coverage using Android Studio?

I am developing apps using Android Studio.我正在使用 Android Studio 开发应用程序。
I was able to run the test code.我能够运行测试代码。
But, I do not know how to get code coverage in android studio.但是,我不知道如何在 android studio 中获得代码覆盖率。

I have already seen the following links.我已经看到以下链接。
Android Gradle Code Coverage Android Gradle 代码覆盖率
But I can't wait for update to v0.6 supporting emma.但我等不及更新到支持 emma 的 v0.6。

Project configuration is as follows.项目配置如下。

Main code主要代码
MyProject/AppName/src/main/java/mypackage/MyClass.java MyProject/AppName/src/main/java/mypackage/MyClass.java

Test code测试代码
MyProject/AppName/src/instrumentTest/java/mypackage/test/MyClassTest.java MyProject/AppName/src/instrumentTest/java/mypackage/test/MyClassTest.java

Project configuration项目配置
MyProject我的项目
├─build.gradle ├─build.gradle
└─AppName └─应用名称
├─build.gradle ├─build.gradle
└─src └─src
├─main ├─主要
│ ├─java │ ├─java
│ │ └─mypackage │ │ └─我的包裹
│ │ └─MyClass.java │ │ └─MyClass.java
│ ├─res │ ├─res
│ └─AndroidManifest.xml │ └─AndroidManifest.xml
└─instrumentTest └─仪器测试
└─java └─爪哇
└─mypackage └─我的包裹
└─test └─测试
└─MyClassTest.java └─MyClassTest.java

With the new Android Studio 1.2, you are able to run your unit tests and see the coverage all within the IDE.使用新的 Android Studio 1.2,您可以在 IDE 中运行单元测试并查看覆盖范围。

First, you'll need to get your unit tests running in the IDE.首先,您需要在 IDE 中运行单元测试。 (if you already can, then skip this step) (如果你已经可以,那么跳过这一步)

This guide and demo will help you.指南演示将为您提供帮助。

Secondly, you'll need to create a JUnit Run configuration其次,您需要创建一个 JUnit Run 配置

在此处输入图片说明

Inside this configuraiton, you'll be able to choose在此配置中,您可以选择

  • Test Kind: "All in Package"测试种类: “全包”
  • Package: [the package where your tests reside, eg: "com.myapp.tests"]包: [您的测试所在的包,例如:“com.myapp.tests”]
  • Search for tests: Across Module Dependencies (could be diff for your setup)搜索测试:跨模块依赖项(可能因您的设置而异)
  • VM -options: -ea虚拟机选项: -ea
  • Working Directory: [your project's directory]工作目录: [你的项目目录]
  • Use classpath of mod: [select your module]使用 mod 的类路径: [选择你的模块]

If you have any issue creating your JUnit Run Configuration, you should visit this guide for help.如果您在创建 JUnit 运行配置时遇到任何问题,您应该访问指南寻求帮助。

Lastly, in the latest Android Studio, you should be able to run your JUnit-Run Configuration by clicking on the 'Run with Coverage' button.最后,在最新的 Android Studio 中,您应该能够通过单击“Run with Coverage”按钮来运行您的 JUnit-Run 配置。


In Android Studio 2.1.3 the is label Run Unit tests with Coverage where Unit test is the name of your test configuration as shown in the following screenshot:在 Android Studio 2.1.3 中,标签Run Unit tests with Coverage其中Unit test是您的测试配置的名称,如下面的屏幕截图所示:

Android Studio:“使用覆盖率运行单元测试”按钮

There are so much answers showing how to apply jacoco plugin to Android studio project, which is outdated, and wasted me so much time to figure out the solution for recently Android studio(My Android Studio is version 2.1.2).有很多答案展示了如何将jacoco插件应用于Android studio项目,已经过时了,浪费了我很多时间来找出最近Android studio的解决方案(我的Android Studio版本是2.1.2)。

  • Jacoco plugin is built in for Android Studio gradle, what you need to do is just enable it like following: Jacoco插件是为 Android Studio gradle 内置的,你需要做的就是像下面这样启用它:
buildTypes {
    ...
    debug {
      testCoverageEnabled true
    }
  }
  • After you do above, run unit test task ./gradlew testDebugUnitTest完成上述操作后,运行单元测试任务./gradlew testDebugUnitTest

  • Then create coverage files: ./gradlew createDebugCoverageReport然后创建覆盖文件: ./gradlew createDebugCoverageReport

  • Coverage files will be created under <module>/build/reports/coverage/debug folder,include index.html , which you can open it with browser, and report.xml which you can use to get a report by jenkins jacoco plugin or other continues integration tools.覆盖文件将在<module>/build/reports/coverage/debug文件夹下创建,包括index.html ,您可以使用浏览器打开它,以及report.xml ,您可以使用它通过jenkins jacoco plugin或其他获取报告继续集成工具。

For those who got 0% coverage with jenkins jacoco plugin , be sure to use the right version.对于那些使用jenkins jacoco plugin获得0% 覆盖率的人jenkins jacoco plugin务必使用正确的版本。 quote from their site :他们的网站报价:

Unfortunately JaCoCo 0.7.5 breaks compatibility to previous binary formats of the jacoco.exec files.不幸的是,JaCoCo 0.7.5 破坏了与以前的 jacoco.exec 文件二进制格式的兼容性。 The JaCoCo plugin up to version 1.0.19 is based on JaCoCo 0.7.4, thus you cannot use this version with projects which already use JaCoCo 0.7.5 or newer.版本 1.0.19 之前的 JaCoCo 插件基于 JaCoCo 0.7.4,因此您不能将此版本用于已经使用 JaCoCo 0.7.5 或更新版本的项目。 JaCoCo plugin starting with version 2.0.0 uses JaCoCo 0.7.5 and thus requires also this version to be used in your projects.从 2.0.0 版开始的 JaCoCo 插件使用 JaCoCo 0.7.5,因此也需要在您的项目中使用此版本。 Please stick to JaCoCo plugin 1.0.19 or lower if you still use JaCoCo 0.7.4 or lower如果您仍然使用 JaCoCo 0.7.4 或更低版本,请坚持使用 JaCoCo 插件 1.0.19 或更低版本

Enable testCoverage in your module build.gradle file在你的模块build.gradle文件中启用 testCoverage

buildTypes {
        debug {
            testCoverageEnabled true
        }
    }

and then接着

Right click on the test -> java package and select Run Tests in Java with Coverage to run all tests with code coverage or right click on the particular test class and click Run SampleTest with Coverage右键单击 test -> java package 并选择Run Tests in Java with Coverage以运行具有代码覆盖率的所有测试或右键单击特定测试类并单击Run SampleTest with Coverage

Have you tried using the Jacoco plugin for getting code coverage for your project?您是否尝试过使用 Jacoco 插件为您的项目获取代码覆盖率? It is a good plugin giving you coverage based on your package or individual classes.这是一个很好的插件,可以根据您的包或单个类为您提供覆盖范围。 I am not sure how you configure Jacoco to use with Gradle since i use Maven.由于我使用 Maven,我不确定您如何配置 Jacoco 以与 Gradle 一起使用。 Check the link : and see if it helps you 检查链接:看看它是否对您有帮助

We use maven to build our app and cobertura for code coverage reporting我们使用 maven 构建我们的应用程序和 cobertura 用于代码覆盖率报告

both are really easy to integrate两者都非常容易集成

android maven integration: android Maven 集成:

http://www.vogella.com/tutorials/AndroidBuildMaven/article.html http://www.vogella.com/tutorials/AndroidBuildMaven/article.html

Maven + Cobertura Code Coverage Example: Maven + Cobertura 代码覆盖率示例:

http://www.mkyong.com/qa/maven-cobertura-code-coverage-example/ http://www.mkyong.com/qa/maven-cobertura-code-coverage-example/

I don't think you can see visual code coverage report inside Android Studio.我认为您无法在 Android Studio 中看到可视化代码覆盖率报告。 But you could try Jacoco .但是你可以试试Jacoco You will need to integrate it in your build.gradle file.您需要将它集成到build.gradle文件中。 You can find the similar question & solution here您可以在此处找到类似的问题和解决方案

Android studio gradle has inbuilt Jacoco plugin which you can use to find code coverage. Android Studio gradle 内置了 Jacoco 插件,您可以使用它来查找代码覆盖率。 I have written as article to step by step configure jaococo to find code coverage for Espresso test case but you can use it for Robotium as well.我已经写了一篇文章,逐步配置 jaococo 以查找 Espresso 测试用例的代码覆盖率,但您也可以将它用于 Robotium。 check this out.看一下这个。

http://qaautomated.blogspot.in/2016/03/how-to-find-code-coverage-with-jacoco.html http://qaautomated.blogspot.in/2016/03/how-to-find-code-coverage-with-jacoco.html

If you want to get your test coverage (for instrumented tests - When the 'Run the app with Coverage' is not enabled):如果您想获得测试覆盖率(对于插装测试 - 未启用“使用覆盖率运行应用程序”时):

Put this into your top-level build.gradle :把它放到你的顶级build.gradle

buildscript{    
       ext.jacocoVersion = '0.8.2'

       ...

        dependencies {
            classpath "org.jacoco:org.jacoco.core:$jacocoVersion"
        }
}

Into your app-level build.gradle :进入你的应用级build.gradle

 ...

    apply plugin: 'jacoco'

    jacoco {
        toolVersion = "$jacocoVersion"
    }

    tasks.withType(Test) {
        jacoco.includeNoLocationClasses = true
    }

    task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

        reports {
            xml.enabled = true
            html.enabled = true
        }

        def fileFilter = [
                '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*', '**/*$[0-9].*'
        ]
        def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
        def mainSrc = "$project.projectDir/src/main/kotlin"

        sourceDirectories = files([mainSrc])
        classDirectories = files([debugTree])
        executionData = fileTree(dir: project.buildDir, includes: [
                'jacoco/testDebugUnitTest.exec', 'outputs/code_coverage/debugAndroidTest/connected/**/*.ec'
        ])
    }


   android {

        ...

        buildTypes {
            debug {
                testCoverageEnabled true
        }
    }

Then you should write your test, and tests have to passed.然后你应该编写你的测试,并且测试必须通过。 If you are sure, your tests passed, write that code into the terminal:如果您确定您的测试通过了,请将该代码写入终端:

gradlew connectedCheck

It will run your tests.它将运行您的测试。 If you did everything right, you should get a report file in app -> build -> reports -> coverage.如果你做的一切都正确,你应该在 app -> build -> reports -> coverage 中得到一个报告文件。 You have to open the index.html file.您必须打开index.html文件。 (Right click on the file -> Open in Browser -> select a browser) (右键单击文件 -> 在浏览器中打开 -> 选择浏览器)

You should get something similar to this.你应该得到类似的东西。 在此处输入图片说明

It's working in my project.它在我的项目中工作。 Maybe there is a better and easier solution.也许有更好更简单的解决方案。 If I forgot something to write down here, pls write comment.如果我忘记在这里写下什么,请写评论。

您可以右键单击您感兴趣的包,然后选择Run 'Tests in "package" with coverage'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM