Testing in Android with AndroidX update

Courtsey: testing.googleblog.com
the three categories of tests that you should include in your app’s test suite

Test types(based on where it runs) and location

If you run local unit tests, a special version of the android.jar (also known as the Android mockable jar) is created by the tooling. This modified JAR file is provided to the unit test so that all fields, methods and classes are available. Any call to the Android mockable JAR results, by default, in an exception, but you can configure Android to return default values.

UI test using Espresso

ViewMatcher : used to locate the view in the UI hierarchy(tree structure of xml layout components) using withId(R.id.id_of_view), withText("find by text on view").

ViewActions : used to perform a specific action or group of actions in the UI views using ViewInteraction.perform(click(),doubleClick()) or click(), longClick(), doubleClick(), swipeDown(), swipeLeft(), swipeRight(), swipeUp(), typeText(), pressKey(), clearText(), etc.

ViewAssertion : used to assert view’s state using ViewInteraction.check(assertion method) where assertion methods can be isDisplayed(), isEnabled(), isRoot().

Espresso Cheat Sheet — View Matcher
Espresso Cheat Sheet — View Action
Espresso Cheat Sheet — View Assertions
android {
defaultConfig {
.... testInstrumentationRunner"androidx.test.runner.AndroidJUnitRunner"
To enable instrumentation testing, add the following dependency
dependencies {
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso- core:3.1.0-alpha4'
androidTestImplementation 'androidx.test:rules:1.1.0-alpha4'

Note:- If you tried to use the latest dependency versions and facing any error, please fork it to the below git link.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store