Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do We Inherit Test Classes Across Android Library Modules?

Tags:

I have an Android Studio project with two library modules: foo-module and bar-module. Each implements a library, with foo-module defining a strategy interface and bar-module depending upon foo-module and implementing such a strategy. foo-module has instrumentation tests (foo-module/src/androidTest/) to test its core code, using a stub strategy implementation, and bar-module should have its own instrumentation tests.

I defined an AbstractTests class in foo-module/src/androidTest/ that does most of the actual testing. I also have a StubTests class in foo-module/src/androidTest/ that extends AbstractTests and implements the necessary abstract methods to complete the test case (providing an implementation of the strategy, etc.). This all works great.

In bar-module/src/androidTest/, I created a BarStrategyTests class, designed to mirror StubTests, but provide the strategy implemented in bar-module. However, BarStrategyTests cannot see AbstractTests, even though I have compile project(':foo-module') in my build.gradle file, and the main (non-test) classes in bar-module can work fine with the main (non-test) classes in foo-module. IOW, while the project() dependency handles regular code, it does not handle androidTest/ code. I get "error: package com.commonsware.foo.test does not exist".

I tried also adding androidTestCompile project(':foo-module'), with the same result.

What is the recipe for sharing instrumentation test code between modules?

Temporarily, I can clone AbstractTests, but that's not a great long-term solution.

This SO question covers similar ground for ordinary Java. Has anyone tried the options in the one answer and gotten them to work for Android instrumentation tests? The first option (move the common testing code into yet another module as regular non-test code) seems plausible, but I have no idea if the other two will work well with the com.android.library plugin instead of the java plugin.

like image 340
CommonsWare Avatar asked May 06 '16 18:05

CommonsWare


People also ask

What base class should test classes inherit from?

Whenever you're testing a subclass of your base class, have your test class inherit from your test superclass. This approach ensures that you haven't changed the behavior of the base class inadvertently, but limits the flexability of your test.

How does inheritance affect software testing?

Inheritance make unit testing of a sub class impossible to do without the super classes methods/variables. Polymorphism causes repeatedly testing same methods. When inheritance combined with the power of polymorphism and dynamic binding, makes it difficult to detect faults that result from the integration of units.


1 Answers

Due to the fact that all the test classes (unit and instrumentation) are not a part of any module, including aar, they are not available via dependency to that module. I faced to this issue as well and solved it by creating test-module and put all the required classes in it (src/main/java). So, in your case you can move AbstractTests in this module and use this module as a androidTestCompile dependency.

like image 65
Oleg Khalidov Avatar answered Sep 20 '22 17:09

Oleg Khalidov