Cut up Android Assessments into modules | Sharing Check Code on Android


We now have all heard about how modularizing your android app into completely different modules is helpful for code maintainability in addition to construct efficiency. So on this weblog, we’re going to discuss how the identical is feasible on your instrumentation assessments.

An android library module in addition to an utility module can have an androidTest supply set in which you’ll write assessments that run on a tool in addition to a take a look at (unitTest) supply set that runs domestically on the JVM. You may be tempted to jot down all of your assessments on the JVM and use frameworks like Robolectric or use Mocks/Stubs wherever you would possibly want some Android framework dependencies as they’re sooner and simpler to execute. However in your utility, you may need a local library that isn’t compiled on your native JVM machine or you could wish to write assessments that take a look at the app on an precise machine, that is when writing assessments inside androidTest supply set is required. Each Builders and QAs in your group can automate the completely different eventualities in your utility by writing assessments contained in the androidTest supply set.

The androidTest supply set on an app module can usually find yourself rising loads in dimension having a whole bunch and even hundreds of assessments. It may be having a mix of screenshot assessments, community assessments, end-to-end workflow assessments, efficiency assessments, and so on… Simply producing an android take a look at APK for a brand new take a look at would possibly take extra time than your regular APK. Should you additionally use plugins like Hilt or Dagger, this could change into worse. It may additionally change into troublesome to resolve which assessments would work domestically with none exterior elements or which assessments rely upon a specific machine or dependency. You would possibly even need to introduce new annotations to handle them successfully.

androidTest supply set has lots of completely different assessments

That is when the androidTest code begins changing into troublesome to handle.

To make the take a look at code extra maintainable, you’ll be able to resolve to separate your android take a look at code into completely different Gradle modules. A technique to do this is to create separate library modules the place you would come with your app as a dependent module. However doing that isn’t so easy, and also you would possibly find yourself going through bizarre manifest merging points. Additionally, you’d be creating fully new library modules the place you don’t want both the important or the take a look at (unitTest) supply set.

That is the place Android Check modules come to the rescue. These are particular modules that solely comprise instrumentation assessments and mean you can embody the appliance and nicely as different library modules as dependencies.

Totally different test-only modules that rely in your app module

To create an Android test-only module, you might want to comply with the next steps:

  1. Create a brand new module in your mission and use the `com.android.take a look at` plugin in that module’s construct.gradle file:
plugins {
// This can be a take a look at solely module.
// All of the assessments inside this could run as Android Instrumentation Assessments.
id 'com.android.take a look at'
}

Be aware: To create a brand new module, you need to use Android Studio’s templates and begin off with a library module as a place to begin after which change com.android.library with com.android.take a look at plugin in your construct.gradle file

2. Specify the mission that you just want to take a look at in opposition to.

android {
// Right here I'm concentrating on the app module.
targetProjectPath ':app'

....
// In case your app module has completely different flavors, you'll have to declare the identical right here.
}

3. In your dependencies block, outline the completely different take a look at in addition to mission dependencies as regular implementation dependencies.

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.materials:materials:1.7.0'
implementation 'junit:junit:4.13.2'
implementation 'androidx.take a look at.ext:junit:1.1.5'
implementation 'androidx.take a look at.espresso:espresso-core:3.5.1'
implementation mission(':app')
}

4. Create your first take a look at like this inside your test-only module.

All of the take a look at code must be contained in the important supply set as an alternative of the androidTest supply set.

class ExampleAppInstrumentedTest {

@get:Rule
val activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)

@Check
enjoyable testAppModuleActivity() {
Espresso.onView(ViewMatchers.withId(com.instance.testonlymodule.R.id.main_activity_view))
.test(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}

You may run all of your instrumentation assessments inside such a module by utilizing a Gradle command like this. (Substitute the moduleName with the precise module identify that you’ve created)

./gradlew :{moduleName}:connectedDebugAndroidTest

That’s it!

That is how simple it’s to arrange a test-only module on your instrumentation assessments.

For an entire working instance of this, you’ll be able to consult with this repo: https://github.com/shubhamgarg1/TestOnlyModule

You should utilize Jacoco with these modules to generate code protection of your utility. You may even run these assessments utilizing headless emulators utilizing Gradle Managed gadgets (Minimal AGP model: 7.3.1).

To share code between your completely different take a look at modules, you’ll be able to create a shared take a look at module that may be included by all of your take a look at modules. The shared take a look at module would must be a library module as Android doesn’t at present mean you can embody test-only modules inside each other.

The identical idea is used whenever you wish to share code between your unit take a look at and android Check supply units. Earlier it was doable by together with the widespread packages in each the supply units. However with Android Studio Chipmunk, that is not doable.

Let’s say there are some shared utilities between community Check and efficiency take a look at modules, you’ll be able to create a Share Check Utility module just like the beneath:

So, that’s a wrap on how one can manage your take a look at code utilizing the com.android.take a look at plugin and the way it will help your take a look at code be extra maintainable and scalable. With this sort of splitting, you’ll be able to resolve to run some take a look at modules after each commit, some take a look at modules as soon as on daily basis, and so forth.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles