As with all software products, the importance of testing in Android development can’t be overemphasized. Users interact with Android applications on a variety of levels, from pressing a button to downloading information onto their device. So, a defect in an application’s functionalities may lead to problems for the user and user dissatisfaction. Hence, it is of the utmost importance that an Android application goes through appropriate testing before release.
UI testing
Integration Testing
Unit testing
UI (User Interface) testing will be discussed in this shot.
As stated earlier, users interact with Android applications on a variety of levels. However, a user is really just concerned about their interaction with the user interface elements that trigger various functionalities of the application. Therefore, it is important to test for the functionalities of all UI components in an Android application, from the click of a button, to navigation, to various screens, etc.
Therefore, we can say that UI testing in Android development ensures that an application’s user interface components meet its functional requirements and achieve a high standard of quality. This way, it is more likely to be successfully adopted by users.
Programmers often shy away from carrying out tests, because of their complexity, and default to manual human testing to perform a set of user operations on the target app. This procedure is often time-consuming and error-prone. A much better approach is to write UI tests, where user actions are performed in an automated manner, as this approach is more reliable.
To write UI tests in Android, the Espresso framework and an Emulator/Physical Android device are utilized.
Espresso has three basic components, namely:
app.gradle
file.java/androidTest
package.setup()
function preceded with @Before
annotation@Test
annotationonView
and withId
)The code snippet below shows how to write and simulate a basic UI test:
@RunWith(AndroidJUnit4ClassRunner::class) class MainActivityTest{ lateinit var firstName: String lateinit var surname: String lateinit var firstNameIcon: String lateinit var phone: String lateinit var email: String @Before fun setup(){ firstName = "Abass" surname = "Adisa" firstNameIcon = firstName.first().uppercaseChar().toString() phone = "08094145784" email = "abasstoy14@gmail.com" val activityScenario = ActivityScenario.launch(MainActivity::class.java) } //This is a sample of a UI test to validate whether all UI components in an activity are displaying as intended @Test fun ui_test_for_mainActivity_components_displays(){ onView(withId(R.id.toolBar_main)).check(matches(isDisplayed())) onView(withId(R.id.addBtn)).check(matches(isDisplayed())) onView(withId(R.id.topView_main)).check(matches(isDisplayed())) onView(withId(R.id.navBtn_main)).check(matches(isDisplayed())) onView(withId(R.id.nextBtn)).check(matches(isDisplayed())) } //This is a sample of a simulated User specific interaction on a form @Test fun ui_test_for_contact_app_to_insert_contact(){ onView(withId(R.id.addBtn)).perform(click()) onView(withId(R.id.firstNameTxt)).perform(typeText(firstName), closeSoftKeyboard()) onView(withId(R.id.surNameTxt)).perform(typeText(surname), closeSoftKeyboard()) onView(withId(R.id.phoneTxt)).perform(typeText(phone), closeSoftKeyboard()) onView(withId(R.id.mailTxt)).check(matches(isDisplayed())) onView(withId(R.id.save)).perform(click()) } }
RELATED TAGS
CONTRIBUTOR
View all Courses