繁体   English   中英

如何在Android中进行工具测试的代码覆盖率

[英]How to get code coverage for instrumentation tests in Android

在Android Studio中,对于在androidTest文件夹下编写的检测测试,我没有找到“以代码覆盖率运行”选项。 但是,对于在测试文件夹中编写的JUnit测试用例,我可以看到它。

谁能告诉我该如何获得保险?

提前致谢。

因此,根据您的评论,我认为您已成功在gradle中设置了jacoco。 现在,请在您的应用gradle中更改或添加以下内容

coveralls {
    jacocoReportPath = "${buildDir}/reports/coverage/debug/report.xml"
}

上面的代码使您的覆盖率报告在app / build / report /中。您将获得两个文件夹,其中包括另一个。 AndroidTest和测试文件夹。 在AndroidTest文件夹中,您将获得coverage文件夹,并浏览index.html,您将获得仪器测试结果的覆盖率报告。 tests文件夹相同,对其进行浏览并浏览index.html

现在主要部分。 您必须在pp gradle中编写以下代码

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

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

    // The lines below make sure we can report against Kotlin and exclude some Android Stuff
    def fileFilter = [
            '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*',
            '**/*Test*.*', 'android/**/*.*'
    ]
    def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug/compileDebugJavaWithJavac/classes/", excludes: fileFilter)
    def mainSrc = "$project.projectDir/src/main/java"

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

成功同步后,您将在任务jacocoTestRepor旁边看到一个播放按钮...单击它,然后选择第一个,例如Run yourProject [jacoco]

暂无
暂无

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

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