The Testing Pyramid: Fast Feedback, Low Cost

The Testing Pyramid guides test suite structure: a wide base of fast unit tests, a smaller middle of integration tests, and a tiny top of slow UI tests. This model is key for Android's local, instrumented, and UI tests. The footgun is inverting it.
WHY IT EXISTS: Test suites with only end-to-end UI tests are slow, expensive to maintain, and flaky. A small change in the UI can break dozens of tests, slowing down development. The Testing Pyramid provides a strategy for getting fast, reliable feedback at a sustainable cost.
THE MENTAL MODEL: Visualize a pyramid. The wide base is made of many small, fast, isolated unit tests. The middle layer has fewer, slightly larger integration tests that check how components work together. The narrow peak has a handful of slow, end-to-end UI tests that verify critical user flows. The higher you go up the pyramid, the slower, more expensive, and more brittle the tests become.
HOW IT WORKS: You structure your test suite according to the pyramid's layers. The base consists of local tests (Unit Tests) that run on the JVM without a device or emulator, testing individual classes or functions in isolation. The middle layer consists of instrumented tests (Integration Tests) that run on a device or emulator to verify interactions between a few components, like a Fragment and its ViewModel. The top layer consists of UI Tests that automate the user interface to test a complete user journey, like a full login flow. The goal is to push logic as far down the pyramid as possible to enable fast, cheap testing.
WHEN TO USE IT: Use this model when designing the testing strategy for any Android application. It provides a framework for deciding what kind of test to write for a given piece of functionality, balancing test coverage with execution speed and maintenance cost.
WHEN NOT TO USE IT: The pyramid is a guideline, not a rigid law. The main thing to avoid is the 'ice cream cone' anti-pattern, where the test suite is dominated by slow, flaky UI tests and has very few unit tests. This leads to a slow, unreliable build process that developers start to ignore.
ONE CANONICAL EXAMPLE: For an Android login screen: write many unit tests for the ViewModel's validation logic (e.g., 'is email valid?'). Write a few integration tests to ensure the Fragment correctly calls the ViewModel. Write one end-to-end UI test that simulates a user typing and tapping the login button to verify the entire flow.
Read the original → developer.android.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.