Skip to content

Expert: features and shipping · Testing Android apps · topic 39 of 50 · 25 min

Testing basics and the test pyramid

Explanation

Tests are code that checks your code automatically. Android has local tests (in src/test, run on your computer's JVM in seconds) and instrumented tests (in src/androidTest, run on a device or emulator). The test pyramid says: many fast unit tests, fewer integration tests, and a small number of slow end-to-end UI tests.

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.

Think of it like this: Tests are like the checklist a pilot runs before every flight. The pilot has flown hundreds of times, but the checklist still runs every time, quickly, because the cost of missing one item is too high.

Two places for tests

Local testsInstrumented tests
Folderapp/src/testapp/src/androidTest
Runs onYour computer (JVM)A real device or emulator
SpeedVery fast (milliseconds)Slower (seconds to minutes)
Good forKotlin logic, ViewModels, repositories with fakesCompose UI, Room on a real database, things that need Android
Run withGutter run icon, or ./gradlew testGutter run icon, or ./gradlew connectedAndroidTest

The test pyramid

output
          /\
         /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)
    /------------\
Most tests should be at the bottom, where they are fast and precise.
  • 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

  1. Business rules that cost money or trust if wrong: prices, taxes, discounts, limits.
  2. ViewModel logic: given these events, the UI state becomes this.
  3. Parsing and mapping: JSON to models, DTO to domain.
  4. 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

Automatic checks

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)

Task 1 · Question

Where do local unit tests live in an Android project?

Task 2 · Practice

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

Question 1

1.Which tests are the fastest?

Question 2

2.What does the test pyramid recommend?

Question 3

3.What is a fake?

Question 4

4.Which command runs local unit tests from the terminal?

Read more (free)

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.

From Testing a full-stack app

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.

From Testing basics: unit, integration and end-to-end

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.

From Integration tests and CI

What do test doubles replace?Related

Answer: Dependencies such as databases, gateways and clocks

You double the edges, not the code you are testing.

From Test doubles: fakes, stubs and mocks

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

  1. 3 more topics in “Testing Android apps” before its exam: Unit tests with JUnit and coroutines, Compose UI tests, Debugging and performance.
  2. Module 10 exam: Testing Android apps · 12 questions · 25 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Publishing on the Play Store and Firebase. 50 topics left in the course before the final exam.

← Previous: Accessibility and adaptive layouts