Better Android Testing at Airbnb — Part 1: Philosophy and Mocking

Testing Philosophy and a Mocking System

Eli Hart
13 min readintermediate
--
View Original

Overview

This article discusses the challenges of automated testing in Android development at Airbnb and introduces the MvRx library as a solution. It emphasizes the importance of a mocking system for effective testing and outlines the architectural changes that facilitate better testing practices.

What You'll Learn

1

How to implement a mocking system for Android UI testing using MvRx

2

Why separating UI and business logic improves testability in Android applications

3

How to use Kotlin data classes for immutable state management in Android

Prerequisites & Requirements

  • Understanding of Android development and testing principles
  • Familiarity with Kotlin programming language

Key Questions Answered

What challenges did Airbnb face with automated Android testing?
Airbnb struggled with automated testing due to flaky tests, high maintenance overhead, and difficulties in mocking network requests. This led to reliance on manual testing, which became impractical as the app grew in complexity.
How does MvRx improve testing in Android applications?
MvRx enhances testing by allowing developers to create a single State class for UI rendering, enabling easy mocking of state for isolated tests. This reduces flakiness and ensures that UI behavior is predictable and consistent during testing.
What is the purpose of the MvRx mocking system?
The MvRx mocking system allows Fragments to define mock implementations of state for their ViewModels, facilitating comprehensive testing with minimal setup. It helps maintain consistency and reduces the complexity of managing UI state during tests.
What are the benefits of using immutable Kotlin data classes in MvRx?
Immutable Kotlin data classes in MvRx ensure that state can only be changed by creating new instances, which simplifies state management and testing. This immutability helps prevent side effects that could lead to flaky tests.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Library
Mvrx
Used for managing UI state and facilitating testing in Android applications.
Programming Language
Kotlin
Used for implementing the MvRx library and managing state in Android applications.

Key Actionable Insights

1
Implement a mocking framework based on MvRx to streamline your Android testing process.
By using MvRx, you can reduce the complexity of your tests and improve their reliability, which is crucial as your application scales.
2
Adopt a single State class architecture to enhance testability and maintainability of your Android applications.
This approach allows for easier state management and reduces the likelihood of side effects during testing, making it easier to catch regressions.
3
Utilize the MvRx Launcher to quickly navigate to any screen in your app for testing.
This tool can significantly speed up the development cycle by allowing developers to test specific states without navigating through the entire app.

Common Pitfalls

1
Failing to separate UI and business logic can lead to complex and untestable code.
This often results in flaky tests and high maintenance overhead, making it difficult to manage changes in a growing application.
2
Overcomplicating mock state definitions can slow down the testing process.
It's important to keep mock states simple and manageable to ensure quick and efficient testing.

Related Concepts

Android Testing
Mocking Frameworks
State Management In UI