Airbnb Eli Hart Dec 13, 2019

Better Android Testing at Airbnb (Part 4: Testing ViewModels)

Article Summary

Airbnb built a testing framework so elegant that their ViewModels tests read like plain English. Here's how they made testing actually enjoyable.

Eli Hart from Airbnb Engineering shares their custom unit testing framework for Android ViewModels. The approach transforms verbose test code into a clean DSL that prioritizes developer experience without sacrificing flexibility.

Key Takeaways

Critical Insight

Airbnb reduced testing friction across hundreds of modules with a DSL that makes ViewModel tests readable and maintainable.

The framework's extensible architecture lets teams add custom assertions for any dependency pattern they encounter.

About This Article

Problem

Eli Hart's team had to test ViewModels across hundreds of Android modules. They needed to keep their approach consistent when verifying state changes and dependency calls, but doing so meant writing a lot of boilerplate code.

Solution

Airbnb built a DSL framework that could be extended for different needs. To test ViewModel functions, you pass in initial state and parameters, then check whether the state changed or verify dependency calls using Mockito. The framework also handles nested state changes and validates network requests.

Impact

The framework made it possible to automatically generate test scaffolding. An IntelliJ IDEA plugin was created that generates test files whenever a new ViewModel is added. This cut down setup time significantly and made it easier for teams across the organization to adopt testing in their large codebase.