Expert: features and shipping · Testing Android apps · topic 39 of 50 · 25 min
Testing basics and the test pyramid
Explanation
Why test?
Every change can break something that used to work: a refactor breaks the GST calculation, a new screen breaks login. Checking everything by hand before each release takes hours and still misses things. A test is a small program that runs part of your app with known inputs and checks the result. Once written, tests run in seconds, every time, and catch breakages before your users do.
Two places for tests
| Local tests | Instrumented tests | |
|---|---|---|
| Folder | app/src/test | app/src/androidTest |
| Runs on | Your computer (JVM) | A real device or emulator |
| Speed | Very fast (milliseconds) | Slower (seconds to minutes) |
| Good for | Kotlin logic, ViewModels, repositories with fakes | Compose UI, Room on a real database, things that need Android |
| Run with | Gutter run icon, or ./gradlew test | Gutter run icon, or ./gradlew connectedAndroidTest |
The test pyramid
/\
/UI\ few: end-to-end UI tests (slow, brittle, but realistic)
/----\
/ Integ \ some: integration tests (repository + fake API, Room + DAO)
/--------\
/ Unit \ many: unit tests (fast, focused, cheap to write)
/------------\- Unit test: checks one small unit (a function, a class) in isolation.
- Integration test: checks several units working together, such as a repository with a fake API and a real Room database.
- End-to-end (E2E) UI test: drives the app like a user, tapping buttons and checking screens.
What to test first
- Business rules that cost money or trust if wrong: prices, taxes, discounts, limits.
- ViewModel logic: given these events, the UI state becomes this.
- Parsing and mapping: JSON to models, DTO to domain.
- A few critical user journeys end to end: login, checkout.
Test doubles: fakes
Real dependencies (network, payment gateway, clock) make tests slow and unpredictable. A test double replaces them. A fake is a simple working implementation, such as a FakeOrderRepository that keeps orders in a list. This is why dependency injection (module 7) matters: a class that receives its dependencies can be given fakes in tests.
Good tests are
- Fast: they run in seconds, so you run them often.
- Independent: each test sets up its own data; order does not matter.
- Repeatable: same result every run (no real network, no real time).
- Readable: a good name says what is checked, for example totalIncludesGstWhenCouponApplied.
Common mistakes
- Only UI tests: slow, flaky, and hard to see what broke. Push logic down into testable classes.
- Tests that hit the real internet: fail whenever the network is slow.
- Testing implementation details (private functions) instead of behaviour.
- Never running tests. Run them before every commit, and in CI.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Testing basics and the test pyramid · 1/6
Tests are code that checks your code automatically, in seconds, every time.
Prefer a video by a teacher? Find free videos on “android testing fundamentals local vs instrumented tests test pyramid” on YouTube ↗
In real life: A coupon bug caught early
A shopping app had a rule: coupons cannot reduce the total below the delivery fee. A developer refactored the pricing class and accidentally removed the check. A five-millisecond unit test, priceNeverBelowDeliveryFee, failed on their laptop before the change was even pushed. Without it, the bug would have reached thousands of orders during a sale.
Practice (0/2 done)
Where do local unit tests live in an Android project?
List 8 things in one of your apps that should be tested. For each, write which level of the pyramid it belongs to (unit, integration or UI) and why.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Unit tests with JUnit and coroutinesTesting Android apps · same module
- Compose UI testsTesting Android apps · same module
- Debugging and performanceTesting Android apps · same module
- Testing a full-stack appFull-Stack Development · Performance, security and testing
- Testing basics: unit, integration and end-to-endFrontend Web Development · Fast, accessible, findable and safe: quality for real sites
- Integration tests and CIFlutter and Dart · Responsive design and testing
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which tests are the fastest?
Answer: Local unit tests
They run on the JVM without a device.
What does the test pyramid recommend?
Answer: Mostly unit tests, fewer integration tests, few UI tests
Fast, focused tests form the base.
What is a fake?
Answer: A simple working replacement for a real dependency in tests
Fakes keep tests fast and predictable.
Which command runs local unit tests from the terminal?
Answer: ./gradlew test
connectedAndroidTest runs instrumented tests.
In the testing pyramid, which kind of test should you have the most of?Related
Answer: Unit tests
Unit tests are fast and cheap, so you can have hundreds. End-to-end tests are slow, so keep only a few key flows.
In the test pyramid, which kind of test should you have the most of?Related
Answer: Unit
Unit tests are fast and cheap, so you can have many. E2E tests are slower, so keep a few for key journeys.
What should integration tests use for login and payments?Related
Answer: Test accounts and a test or fake backend
Never use real money or data.
What do test doubles replace?Related
Answer: Dependencies such as databases, gateways and clocks
You double the edges, not the code you are testing.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Testing Android apps” before its exam: Unit tests with JUnit and coroutines, Compose UI tests, Debugging and performance.
- Module 10 exam: Testing Android apps · 12 questions · 25 min, without help. 4 topics of this module still to finish first.
- Then the next module, Publishing on the Play Store and Firebase. 50 topics left in the course before the final exam.