gpt4 book ai didi

android - Jacoco 覆盖率报告问题

转载 作者:可可西里 更新时间:2023-11-01 18:47:52 30 4
gpt4 key购买 nike

我正在尝试定义位置,jacoco 将为在真实设备上运行的仪器测试创建覆盖文件。

来自--debug运行 gradle 任务我看到这个日志:

[DEBUG] [org.gradle.api.Task] DeviceConnector 'Nexus 5X - 6.0.1': installing /home/martin/workspace/lib/my-lib/build/outputs/apk/my-lib-debug-androidTest-unaligned.apk
[INFO] [org.gradle.api.Task] Starting 1 tests on Nexus 5X - 6.0.1
[INFO] [org.gradle.api.Task] de.my.lib.utils.UtilsTest testMyTest[Nexus 5X - 6.0.1] [32mSUCCESS [0m
[DEBUG] [org.gradle.api.Task] DeviceConnector 'Nexus 5X - 6.0.1': fetching coverage data from /data/data/de.my.lib.test/coverage.ec
[DEBUG] [org.gradle.api.Task] DeviceConnector 'Nexus 5X - 6.0.1': uninstalling de.my.lib.test 13:46:14.538
[DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':my-lib:connectedDebugAndroidTest'

我尝试了 3 种方式来定义位置:

使用 <instrumentation> list 文件中的标签没有任何改变。

<?xml version="1.0" encoding="utf-8"?>
<manifest
package="de.my.lib.test"
xmlns:android="http://schemas.android.com/apk/res/android">

<instrumentation
android:name="android.support.test.runner.AndroidJUnitRunner"
xmlns:tools="http://schemas.android.com/tools"
android:targetPackage="de.my.lib.test"
tools:replace="android:targetPackage">
<meta-data
android:name="coverage"
android:value="true" />
<meta-data
android:name="coverageFile"
android:value="/sdcard/coverage.ec" />
</instrumentation>
</manifest>

我用 gradle 试了一下,但输出是一样的:

defaultConfig {
// unimportant stuff
testApplicationId "de.my.lib.test"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArgument('coverageFile', '/sdcard/coverage.ec')
}

最后我用 adb 试了一下命令:

adb shell am instrument -w -e coverage true -e coverageFile /sdcard/coverage.ec de.my.lib.test/android.support.test.runner.AndroidJUnitRunner

但是我得到了 2 个错误:

de.my.lib.utils.UtilsTest:. Could not find class: org.jacoco.agent.rt.internal_773e439.CoverageTransformer . Time: 0,072

OK (1 test)

Error: Failed to generate emma coverage.

我完全迷失在这里。有什么想法吗?

背景 为什么我需要将它存储在另一个地方:adb shell run-as 有一个错误命令在某些设备和 Android 版本上,所以我的测试设备场中有设备返回 0% 的覆盖率,因为无法提取文件。所以我需要将该文件存储在一个公开可用的位置。

最佳答案

However, the coverage report is not generated yet. To enable this option, we need to add a property to our debug build variant. Using the Android plugin DSL, we can enable the coverage through the testCoverageEnabled property:

android {
...
buildTypes {
debug {
testCoverageEnabled true
}
...
}
}

To enable the coverage report for local tests when using version 2.2.+ of Android Gradle plugin, you need to enable it in your app’s build.gradle:

android {
//...

testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
}

The instrumentation performed by Jacoco produces execution files that contain the necessary data to create the report (HTML, XML, etc.). The problem here, is that Espresso generates .ec file, while the unit tests execution generates .exec file… we have different formats!

As we are not able to configure the coverage task of Espresso, we must ensure that it is executed first. Next, we need to run unit tests, and then create the coverage data with both files (ec and exec).

To enable this, we need to edit our task once more and add the coverage.ec file as a parameter in executionData property:

apply plugin: 'jacoco'

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

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

def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"

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

As Android Gradle plugin 2.2.+ now generates a coverage file for each execution, using the device / emulator in the file name, now we need to pass every file to execution data, as the file name is now dynamic. In addition, I added the createDebugCoverageReport task as a dependency of our custom task, so we don’t need to run it manually :)

Finally, when we execute both tasks, running the Espresso tests first, we will get the unified coverage report!

gradle clean jacocoTestReport

详细解释见here .

编辑:

因此,命令 adb shell am instrument 不会生成报告,为此您必须使用 gradle 解决方案来处理它。如果你真的想自己测试一下,那么你必须使用 custom runner .

引用资料:

1) How to Generate Android Testing Report in HTML Automatically

2) How to retrieve test results when using "adb shell am instrument"

3) https://github.com/jsankey/android-junit-report


Test from the command line


更新为命令行方法遵循 these步骤。

运行检测应用程序

一旦我们有了 EmmaInstrumentation 类(在 ref 链接中),我们需要进行一些更多的调整才能获得正在运行的应用程序的覆盖率报告。

首先,我们需要将新的 Activity 添加到 list 中。其次,如果这是我们决定生成覆盖率报告的地方,我们应该允许我们的应用程序写入 sdcard。为此,您应该授予 android.permission.WRITE_EXTERNAL_STORAGE 权限。然后,是时候构建和安装检测过的 apk 了:

$ ant clean
$ ant instrument
$ ant installi

一切准备就绪,可以启动检测应用程序

$ adb shell am instrument -e coverage true \
-w com.example.i2at.tc/\
com.example.instrumentation.EmmaInstrumentation

Here是源代码。

关于android - Jacoco 覆盖率报告问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35202239/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com