Share code between test modules
In 2015 I wrote a post on how to share code between unit tests and instrumentation tests in Android. Ian Cunningham I simply identified that the strategy I described it no longer works.
That is effective with me as a result of I have never used that technique in years! At present I exploit modules, which offer a far superior technique of fixing the issue (and it is the strategy that Google now promotes as effectively). Is that the way it works:
- Begin with a module referred to as
:library
. - Create a second module referred to as
:library-shared-test
and put:library
as a dependency on it. - Deploy your shared take a look at utilities to
:library-shared-test
. - Now here is the trick: add
:library-shared-test
as a dependency to your exams on:library
.
Though it appears to be like such as you’re making a round dependency in step 4, you are not. As an alternative you’re making a dependency chain out of :library
-> :library-shared-test
-> :library
The take a look at of
I’ve put a demo project here if that helps your understanding.
There may be one huge benefit that this technique has over the earlier one: none module can use :library-shared-test
now. Sure :library-shared-test
comprises helpful utilities or fakes, whichever you devour :library
you can too attempt utilizing these now too.
It ought to be fairly easy to go from the outdated technique to the brand new one, and also you get just a few bonuses alongside the best way. Go for it!
Edit: after posting, zac sweets identified to me that there’s a gradle test accessory plugin which mainly does the identical factor as described above, however higher. Still doesn’t work in Android Kotlin projectsso the recommendation above remains to be good for now, however for non-kotlin-android initiatives it’s best to most likely use it as a substitute.