简体   繁体   English

通过Gradle命令在代码覆盖范围内运行Android测试

[英]Running Android Tests with code coverage through Gradle command

Without using JaCoCo, is there a way to run code coverage on my tests through Gradle commands? 如果不使用JaCoCo,是否可以通过Gradle命令在测试中运行代码覆盖率? There does not appear to be a Gradle task which runs tests with coverage on. 似乎没有Gradle任务可以在覆盖范围内运行测试。 The only option I see is a button on the Android Studio UI which will run my tests with code coverage. 我看到的唯一选项是Android Studio UI上的一个按钮,它将以代码覆盖率运行我的测试。

Ideally I'd like to automate my tests with code coverage from command line and access the report it generates. 理想情况下,我想使用命令行中的代码覆盖范围自动化我的测试,并访问它生成的报告。

By the way, I do not have anything against JaCoCo. 顺便说一句,我对JaCoCo没有任何反对。 I just want to know if there is a way to do this without using the library. 我只想知道是否有一种无需使用库即可执行此操作的方法。

edit: Possibly related but didn't the solution did not work for me - testDebug is not a task for me. 编辑: 可能相关,但是解决方案对我不起作用-testDebug 对我来说不是任务。

edit 2: Google Issue Tracker which seems to be related to my question. 编辑2: Google问题追踪器 ,似乎与我的问题有关。 Opened over a year ago. 一年多以前开业。

edit 3: Appears that this is also an issue when trying to use Jacoco... There does not appear to be a gradle task provided by jacoco to do unit tests in the src/test directory from my research. 编辑3:在尝试使用Jacoco时,这似乎也是一个问题。根据我的研究,jacoco似乎没有提供gradle任务来在src / test目录中进行单元测试。 Would love to be proven wrong! 希望被证明是错误的!

I was able to solve my problem after much research into how Gradle works and thanks to some examples such as here and from this github project here . 我可以多研究摇篮是如何工作的,并感谢一些例子,如后解决我的问题在这里 ,并从该github上的项目在这里 I needed to make a few tweaks to get it to work with my project, but the result is that I can run code coverage on JUST my unit tests found in the src/test/ directory from the command line, and access those generated reports. 我需要进行一些调整才能使其与我的项目一起使用,但是结果是,我可以在命令行中的src / test /目录中找到我的单元测试,从而运行代码覆盖率,并访问生成的报告。

Here's what the task looks like in my build.gradle: 这是我的build.gradle中的任务:

task jacocoReport(type: JacocoReport, dependsOn: 'testMockDebugUnitTest') {
    group = 'Reporting'
    description = 'Generate Jacoco coverage reports after running tests.'

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

    classDirectories = fileTree(
            dir: 'build/intermediates/classes/test/mock/debug',
            excludes: [
                    '**/R*.class',
                    '**/BuildConfig*'
            ]
    )

    sourceDirectories = files('src/main/java')
    executionData = files('build/jacoco/testMockDebugUnitTest.exec')

    doFirst {
        files('build/intermediates/classes/test/mock/debug').getFiles().each { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

Changes that I needed to make: Running my unit tests alone is done by the gradle task testMockDebugUnitTest , and the jacocoReport task needed to rely on my unit tests being completed first. 我需要进行的更改:单独运行单元测试是由gradle任务testMockDebugUnitTest ,而jacocoReport任务需要首先完成我的单元测试。 The generated build tree for me is a little different, so I hardcoded it in. Execution data that Jacoco relies on is found in the .exec file generated by the testMockDebugUnitTest task, which also needs to be defined. 对我而言,生成的生成树略有不同,因此我对其进行了硬编码。Jacoco所依赖的执行数据在testMockDebugUnitTest任务生成的.exec文件中找到,该文件也需要定义。

After all is said and done, I can now get code coverage for my unit tests separately from my instrumentation tests through command line. 说完一切之后,我现在可以通过命令行从单元测试中获得单元测试的代码覆盖率。

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

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