Generate coverage for other modules

痴心易碎 提交于 2019-12-23 13:06:40

问题


My project structure is as follows: :app, :core. :app is the Android application project, and it depends on :core which has all the business logic.

I have Espresso tests for :app, and I am able to run and get coverage report thanks to all the questions and guides out there. But the coverage is only for code in :app.

How do I get coverage for all projects (:app and :core) resulting from my Espresso instrumentation tests? Is this even possible?

Any help is greatly appreciated.


回答1:


Even if you did not mention Jacoco in your question, it is listed in the tags, so I assume you want to create coverage reports by using it. The Gradle Jacoco plugin (apply plugin: 'jacoco') allows you to create custom tasks of the type JacocoReport.

Defining such a task, you can specify source and class directories as well as the execution data:

task jacocoTestReport(type: JacocoReport) {
    dependsOn // all your test tasks
    reports {
        xml.enabled = true
        html.enabled = true
    }
    sourceDirectories = // files() or fileTree() to your source files
    classDirectories = // files() or fileTree() to your class files
    executionData = // files() or fileTree() including all your test results
}

For Espresso tests, the execution data should include all generated .ec files. You could define this task in the Gradle root project and link to both the files and the tasks of your subprojects.

There is an example for a custom Jacoco report task, even if it aims on creating an unified report for multiple test types, it can be transfered for this multi-project problem.




回答2:


By default the JacocoReport task only reports coverage on sources within the project. You'll need to set the additionalSourceDirs property on the JacocoReport task.

app/build.gradle

apply plugin: 'java'
apply plugin: 'jacoco'

// tell gradle that :core must be evaluated before :app (so the sourceSets are configured)
evaluationDependsOn(':core') 

jacocoTestReport {
    def coreSourceSet = project(':core').sourceSets.main
    additionalSourceDirs.from coreSourceSet.allJava
    additionalClassDirs.from coreSourceSet.output
}


来源:https://stackoverflow.com/questions/43057920/generate-coverage-for-other-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!