Test-Driven Development in Android MVVM Architecture(Part 3-Espresso)
Espresso is an open-source android user interface (UI) testing framework developed by Google.
What is Espresso?
Espresso is a simple, efficient, and flexible testing framework. Here, we gonna setup the Espresso framework in a project, the workflow of the framework, and finding, automating & asserting user interface components in the testing environment with the simple android application.
Espresso basically has 3 components
- ViewMatchers — allows finding view in the current view hierarchy
- ViewActions — allows performing actions on the views
- ViewAssertions — allows asserting the state of a view
Outline Structure:onView(ViewMatcher)
.perform(ViewAction)
.check(ViewAssertion)
Before begin, if you didn’t check with my previous article, I recommend you check it here.
We have a small sample android application that hosts a Login screen and Home screen. You can clone it here. I recommend you clone this repo and checkout to tdd-unit-testing
branch, so you can easily understand the flow ✌.
git clone https://github.com/shehzab.developer/Test_Driven_Development.git
git checkout -b tdd-unit-testing
Let’s start step by step…
Step 1: Add below dependencies into your app module’s build.gradle.
Espresso support:androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Espresso intent mocking support:androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
ActivityTestRule:androidTestImplementation 'androidx.test:rules:1.2.0'
Step 2: Its recommended to turn off animations in the android device which is used for testing.
Step 3: Open your project, goto package login
, open LoginActivity.kt
.
Step 4: Right-click LoginActivity.kt
and select Generate > test
LoginViewModel and select Generate
Step 5: Pop-up appears. Select default values (cross-check with this image -left side) and click Ok.
Step 6: Pop-up appears. Select an androidTest and click Ok.
Notice the generated LoginActivityTest
class in androidTest/login/
package.
Step 7: Now we need to add @RunWith(AndroidJunit4::class) on class name and ActivityTestRule which provides functional testing of single Activity.
So now code looks like:
@RunWith(AndroidJUnit4::class)
class LoginActivityTest{ @get:Rule
var activityTestRule = ActivityTestRule(LoginActivity::class.java)
}
Step 8: Now we ill write a simple test case to check whether username edit text is visible or not.
Write a method named as isUsernameEditTextDislayed
with @Test
annotation. Copy-paste below code snippet.
@Test
fun isUsernameEditTextDisplayed() {
activityTestRule.launchActivity(Intent())
onView(withId(R.id.etUsername)).check(matches(isDisplayed()))
}
In the above code, I am calling username edit text Id inside ViewMatcher and checking is it visible or not with ViewAssertion.
Run the test. Cool… It passed. For the running test, we need a physical device or emulator.
Espresso methods can be self-explanatory.
Step 9: Now we ill write test cases for a user entering username and password and click on the Login button. Followed by HomeActivity opens.
@Test
fun isUserLoggedAndHomeScreenCalled() {
activityTestRule.launchActivity(Intent())
//entering username
onView(withId(R.id.etUsername)).perform(
ViewActions.typeText("android"),
ViewActions.closeSoftKeyboard()
)
//entering password
onView(withId(R.id.etPassword)).perform(
ViewActions.typeText("123456"),
ViewActions.closeSoftKeyboard()
) //checking HomeActivity is called
Intents.init()
onView(withId(R.id.btnLogin)).perform(ViewActions.click())
Intents.intended(IntentMatchers.hasComponent(HomeActivity::class.java.name))
Intents.release()
}
Run the test. Cool… It passed ✌.
Awesome 👏👏👏. We just completed the basic intro to testing. Don’t stop here there is more to learn.
For complete source code for this chapter, checkout repo to tdd-espresso.git checkout -b tdd-espresso
Thank you for your time to read this article. If you like it please give me your clap 👏 , so others can find it too.