theapache64
theapache64

Reputation: 11754

I am confused when to use instrumentation test and when to use unit test

With AndroidX Test Framework now we can run Espresso test as unit test using Robolectric backend . But am confused when to use instrumentation test and when to use unit test.

Let's say we've two screens, MovieListActivity and MovieDetailActivity. when I click on an item in the MovieListActivity, details of that movie will be shown in the MovieDetailActivity.

Now with this scenario, what could be the possible unit tests and what could be the possible instrumentation tests?

For example, I can write instrumentation test to

but now, these tests can be run as unit tests too, (since we're using Android X Test Framework) plus it's faster, as we're not running the test in a device/emulator. So what makes a test eligible for the instrumentation test?

What if I want to run a test as both unit test and instrumentation test?

As we're using Android X Test Framework the same unit test file can be run as an instrumentation test by copying the file to androidTest folder. This makes duplicate file and makes the test case maintenance hard. (For example if I make an edit in the unit test folder, the same modification should be done in androidTest file.)

Upvotes: 2

Views: 1453

Answers (1)

Chetan Gupta
Chetan Gupta

Reputation: 1647

I use a basic strategy where I use, Android X Test | Robolectric for writing unit test that needs context, like all the new jetpack component are very tightly coupled with the framework we need context to work with them, so Robolectric will work great, no need for androidTest Instrumentation but if I'm working with Views that needs me to check set feature of a view and make it visible on my ui with some fancy animation or custom thing I use espresso Instrumentation.

I'm not getting into details but I will be creating a supporting blog for the explanation, but Hackish Cheatsheet of Test and Framework IMO would be

Remote DataSource Testing [Retrofit] - Mockserver | Fakes | Mocks
Local DataSource database [Room] - Android X Test | Robolectric 
Local DataSource Prefs [SharedPrefernces] - Android X Test | Robolectric 
Local DataSource Store [DataSource] - Android X Test | Robolectric 
Repository Layer | UseCases | business-logic  - JUnit 4/5 | Fakes | Mocks
ViewModel & LiveData - Android X Test | Robolectric 
Navigation [Navhost|Intents] - Android X Test | Robolectric 
View & Animations [Activity|Fragments|CustomViews] - Espresso|Barista Instrumentation
Notifications Test - Instrumentation and ADB
Broadcast Test - Instrumentation and ADB
Content provider - Instrumentation and ADB
Firebase Testing(depends on things you are testing) - Espresso|Barista Instrumentation | Android X Test | Robolectric | ADB

Do let me know if you have separate experince.

Upvotes: 4

Related Questions