War*_*ith 15 android code-coverage gradle jacoco test-coverage
我正在尝试定义位置,jacoco将为真实设备上运行的检测测试创建覆盖文件.
从--debuggradle任务的运行中我看到这个日志:
[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'
Run Code Online (Sandbox Code Playgroud)
我尝试了三种方法来定义位置:
使用<instrumentation>清单文件中的标记不会改变任何内容.
<?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>
Run Code Online (Sandbox Code Playgroud)
我用gradle尝试了它,但输出是相同的:
defaultConfig {
// unimportant stuff
testApplicationId "de.my.lib.test"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArgument('coverageFile', '/sdcard/coverage.ec')
}
Run Code Online (Sandbox Code Playgroud)
最后我用adb命令尝试了它:
adb shell am instrument -w -e coverage true -e coverageFile /sdcard/coverage.ec de.my.lib.test/android.support.test.runner.AndroidJUnitRunner
Run Code Online (Sandbox Code Playgroud)
但在那里我得到2个错误:
de.my.lib.utils.UtilsTest :. 找不到类:org.jacoco.agent.rt.internal_773e439.CoverageTransformer.时间:0,072
好的(1个测试)
错误:无法生成emma覆盖率.
我完全迷失在这里.有任何想法吗?
背景为什么我需要它将它存储在另一个地方:adb shell run-as某些设备和Android版本上存在命令错误,因此我的测试设备场中的设备返回0%覆盖率,因为无法提取文件.所以我需要将文件存储在公共场所.
小智 3
\n\n\n但是,覆盖率报告尚未生成。要启用此选项,我们需要向调试构建变体添加一个属性。使用Android插件DSL,我们可以通过属性启用覆盖
\ntestCoverageEnabled:
android {\n ...\n buildTypes {\n debug {\n testCoverageEnabled true\n }\n ...\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n要在使用2.2.+版本的 Android Gradle 插件时启用本地测试的覆盖率报告,您需要在 app\xe2\x80\x99s 中启用它
\nbuild.gradle:
android {\n //...\n\n testOptions {\n unitTests.all {\n jacoco {\n includeNoLocationClasses = true\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\nJacoco 执行的检测会生成包含创建报告所需数据(HTML、XML 等)的执行文件。这里的问题是,Espresso 生成
\n\n.ec文件,而单元测试执行生成.execfile\xe2\x80\xa6 我们有不同的格式!由于我们无法配置Espresso的覆盖任务,因此我们必须确保它首先被执行。接下来,我们需要运行单元测试,然后使用两个文件(
\n\nec和exec)创建覆盖率数据。要启用此功能,我们需要再次编辑任务并将
\ncoverage.ec文件添加为属性中的参数executionData:
apply plugin: \'jacoco\'\n\ntask jacocoTestReport(type: JacocoReport, dependsOn: [\'testDebugUnitTest\', \'createDebugCoverageReport\']) {\n\n reports {\n xml.enabled = true\n html.enabled = true\n }\n\n def fileFilter = [\'**/R.class\', \'**/R$*.class\', \'**/BuildConfig.*\', \'**/Manifest*.*\', \'**/*Test*.*\', \'android/**/*.*\']\n def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)\n def mainSrc = "${project.projectDir}/src/main/java"\n\n sourceDirectories = files([mainSrc])\n classDirectories = files([debugTree])\n executionData = fileTree(dir: "$buildDir", includes: [\n "jacoco/testDebugUnitTest.exec", \n "outputs/code-coverage/connected/*coverage.ec"\n ])\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\nAs 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
\n \ncreateDebugCoverageReporttask as a dependency of our custom task, so we don\xe2\x80\x99t need to run it manually :)Finally, when we execute both tasks, running the Espresso tests first, we will get the unified coverage report!
\n
gradle clean jacocoTestReport\nRun Code Online (Sandbox Code Playgroud)\n\nFor detailed explanation see here.
\n\nSo, the command adb shell am instrument does not generates the report, for this you have to handle it using gradle solution. If you really want to test it your self, then you\'ve to use a custom runner.
References:
\n\n1) How to Generate Android Testing Report in HTML Automatically
\n\n2) How to retrieve test results when using "adb shell am instrument"
\n\n3) https://github.com/jsankey/android-junit-report
\n\nUpdated to Command line method follow these steps.
\n\nRunning the instrumented application
\n\nOnce we have the EmmaInstrumentation class (is in ref link) in place we need a few more adjustments to be able to get the coverage report of the running application.
Firstly, we need to add the new Activity to the manifest. Secondly, we should allow our application to write to the sdcard if this is where we decided to generate the coverage report. To do it you should grant the android.permission.WRITE_EXTERNAL_STORAGE permission.\nThen, it\'s time to build and install the instrumented apk:
\n\n$ ant clean\n$ ant instrument\n$ ant installi\nRun Code Online (Sandbox Code Playgroud)\n\n一切准备就绪,可以启动仪表化应用程序
\n\n$ adb shell am instrument -e coverage true \\\n -w com.example.i2at.tc/\\\n com.example.instrumentation.EmmaInstrumentation\nRun Code Online (Sandbox Code Playgroud)\n\n这是源代码。
\n| 归档时间: |
|
| 查看次数: |
1278 次 |
| 最近记录: |