问题
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