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?
I use a basic strategy where I use,
Android X Test | Robolectricfor writing unit test that needscontext, like all the new jetpack component are very tightly coupled with the framework we need context to work with them, soRobolectricwill work great, no need forandroidTest Instrumentationbut if I'm working with Views that needs me to checksetfeature 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
Do let me know if you have separate experince.