问题
I inherited an android project to setup code coverage for. Not having done much for android and almost as little in gradle, I embarked on a quest to find a helpful tutorial. As surprises go, the first few tutorials were very helpful and I was able to include the jacoco gradle plugin and enable the code coverage. Using jenkins I even generated a coverage report. So far everything looks fine.
However, upon setting my eyes on the report, I smelled something fishy. The test vs coverage ratio seemed to be far too small. Further investigation revealed the culprit.
The tests itself are written more as functional not unit ones. That would be ok. However, the project library has no tests in its module. Instead the library tests are written in the gui module (as that is where the library is used).
Therefore, even though most of the library functionality is covered by tests, coverage is generated for stuff from gui module only.
Project
-- Gui module
---- gui sources
---- all the tests
-- Library module
---- library sources
No I have been looking for a working solution quite some time. Unfortunately, all I was able to find was how to combine unit and integration .exec test coverage results into one report (or other unit test based solutions - none of which worked for the instrumentation ones).
What I need, is generate coverage for sources from Library module based on Gui module tests.
As I am stumbling in a dark here, is even anything like that, remotely possible?
回答1:
For anyone reading this... if you have the same issue, it is time to start banging your head against the wall...
Today I was lucky enough to stumble upon this: https://issuetracker.google.com/issues/37004446#comment12
The actual "problem" seems to be, that library projects are "always" of release type. Therefore they do not contain "necessary instrumentation setup" (unless you enable code coverage for release as well, although I haven't tested it).
So the solution is to specifically enable, in the library to be published, "debug" build (as mentioned, default is the release type):
android {
publishNonDefault true
}
Then, in the project that uses library, specify a debugCompile dependency (release compile can use the "default" release configuration):
dependencies {
debugCompile project(path: 'library', configuration: 'debug')
releaseCompile project('library')
}
And of course (this one I take for granted), remember to enable test coverage for the library:
android {
buildTypes {
debug {
testCoverageEnabled true
}
}
}
来源:https://stackoverflow.com/questions/45616769/android-instrumentation-tests-for-library-module-coverage