Getting started with Test-Driven Development (TDD)

Note: This series is for advanced Android Developers. Are you a beginner? See our YouTube tutorials or other articles on this publication.

Most developers love to automate their work. Why test something for 30 minutes when it can take 10 seconds? As a developer, you need to ensure that your Android app is working 100% of the time. But testing the entire app after every code change can be difficult. That’s where Test-Driven Development comes into play. What is TDD?

TDD on YouTube.

TDD is a process for writing code that start with failed tests and ends with functional code. Here is an example test in Android Studio:


@Test
fun when_user_enters_first_and_last_name_check_to_confirm_that_message_is_correct() {
onView(withId(R.id.firstName)).perform(typeText("Jake"))
onView(withId(R.id.lastName)).perform(typeText("Smith"))
onView(withId(R.id.button)).perform(click())
onView(withId(R.id.message)).check(matches(withText("Welcome, Jake Smith!")))
}

Try reading the code above. Did any of it make sense? Even if you’ve never seen an Espresso test before, you may be able to understand keywords like R.id.firstName and perform and typeText. What does this Espresso test do?

First, it finds an EditText with the Id of firstName, then the test types “Jake” in that EditText. After a couple more lines, we check to see that the Id with message matches “Welcome, Jake Smith!”

For a complete walkthrough of the above code, see our YouTube video on Test-Driven Development.

Part two of this series will be available next Saturday at 8am.

Android and RxJava: Using a Single

  • Use RxJava's Single class when the expected type is either a success or error.
  • It's only slightly different from Observable.just(), which we discussed in the last article.

2 days ago

Android and RxJava: Getting Started

  • The powerful library RxJava can help developers build better apps.
  • See how to use Observables and Observers in this tutorial.

2 days ago

Android RecyclerView with Kotlin - Part 1

  • Oftentimes, developers need to display a list of some sort of data.
  • Whatever type of data is displayed, you should use a RecyclerView.

6 days ago