Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android instrumentation tests for library module coverage

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?

like image 249
Viliam Aboši Avatar asked Aug 10 '17 14:08

Viliam Aboši


People also ask

How do I get Android test coverage?

Android Studio has a built-in feature that allows you to run tests with code coverage. Simply navigate to the src/test/java folder and right click. Then select Run 'Tests in 'java'' with Coverage (awkward use of single quotes theirs not mine).

What are instrumentation tests in Android?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.

What is instrumentation in mobile testing?

Instrumentation is a process to prepare the application for testing or automation. Part of the instrumentation process may add "instruments" that allow the testing framework to gain access to parts of the application. Perfecto provides tools for instrumenting mobile applications for different purposes.


1 Answers

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 
                } 
        } 
} 
like image 186
Viliam Aboši Avatar answered Oct 17 '22 21:10

Viliam Aboši