Android app generates test coverage report

For Android integration created with AndroidJUnit4 runner Test cases , which have never been found before, are a suitable way to generate code coverage. I have tried many ways, but either it doesn't work, or it is only suitable for the Robolectric test framework that I no longer use, and other developers gradually stop using this test framework in new projects. For example, Square's Sqlbrite project has now begun to use AndroidJUnit4 runner to test their projects. Recently, I found an interesting discussion on Reddit, from which I discovered a simple way to generate code coverage without additional plugins, scripts and multiple lines of configuration. The Android SDK now has built-in support for the Emma Test Coverage framework. Support, can be consulted in the official documentation.
  All we need to do is apply the jacoco-android plugin in build.gradle.
applyplugin:'jacoco-android'
  Translator's Notes
  In the higher version of the SDK, the above plug-ins do not need to be referenced
  Then refer to the following example, set testCoverageEnabled to true
  android {
     buildTypes {
        debug {
           testCoverageEnabled = true
        }
     }
  }
  To be able to generate the code coverage report, we need to connect the Android device or emulator to the computer, because before generating the report, the connectedCheck task will be executed.
  After that, we can execute the following command line
./gradlew createDebugCoverageReport
  The task analyzes /src android {
     buildTypes {
        debug {
           testCoverageEnabled = true
        }
     }
  }/main/java/ and unit .
  After performing this task, we can find the code coverage report in the following path of the module
/build/outputs/reports/coverage/debug/
  We can open the index.html file in the browser and see the visual report. 
  At the same time, in the same level directory, we can also find the report.xml file that can be used for continuous integration coverage analysis.
  In addition to the files mentioned above, Gradle will also create the coverage.ec file in the following path.
/build/outputs/code-coverage/connected/
  In some cases, we may need these files, such as when choosing a Jenkins plugin or other tool to generate code coverage.
  Below, you can see a code coverage report for an Android open source project, Presfer.
  This is a report generated by the JaCoCo code coverage library.
  Through the analysis report, I added some new test cases and slightly optimized some codes, so that the coverage of the project reached 100%.
  为了能在 Jenkins CI 上发布报告,可以使用代码覆盖率插件,但是并不能确认插件的稳定性。另一种解决方案是 HTML Publisher plugin,我们可以增加相应动作,在 Jenkins 的任务中,通过默认的HTML界面产生覆盖率报告,我认为这是一种非常方便的方式,易于创建,并且方便进行代码导航,能定位到没有覆盖的代码行,方法和分支。

  我们可以方便的观察到Android项目的代码覆盖情况通过这种简洁快捷的方式,能帮助我们最终发现项目的瓶颈所在,增加应用或者lib库的整体质量。

如果对软件测试有兴趣,想了解更多的测试知识,可以加入我的QQ群  高级测试学习大家庭:652068511

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325846645&siteId=291194637