Skip to content Skip to sidebar Skip to footer

Generate Coverage For Other Modules

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,

Solution 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.

Solution 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
}

Post a Comment for "Generate Coverage For Other Modules"