2

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

  • Check if the movies are listed
  • Clicking on the movie goes to detail screen
  • Detail screen displays the details correctly.

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.)

  • Is there any way I can mark a test to run as both instrumentation test and unit test?
  • If it's a bad practice, what's the best practice and what are other best practices should be considered when using Android X Test Framework?
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
theapache64
  • 10,926
  • 9
  • 65
  • 108
  • 1
    The simple answer: you use unit tests whenever no need to use android APIs, but when you need to use android APIs like manipulating activity, widgets, stuff like `TextView`, `EditText`.. you've to use instrumental tests under `androidTest` package – Zain Apr 26 '20 at 15:31
  • 1
    @Zain simple and concise answer! – Nicola Gallazzi Apr 26 '20 at 15:32
  • @Zain Yeah that makes sense, but what am asking is, when it comes to `AndroidX Test Framework` with `Robolectric`, on what basis the test type should be chosen? – theapache64 Apr 26 '20 at 15:51

1 Answers1

4

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.

Chetan Gupta
  • 1,477
  • 1
  • 13
  • 22