Book a free call

Testing Android Apps with Espresso on AWS Device Farm

Espresso writes the UI tests and AWS Device Farm runs them on real devices. Here is how the pair works, how to set it up, and what it costs in time.

Hertiv engineering teamReviewed by a senior engineer
6 min read
Testing Android Apps with Espresso on AWS Device Farm
On this page

Short answer

Testing an Android app with Espresso on AWS Device Farm means writing UI tests with Google's Espresso framework, then running them on real devices hosted by AWS instead of on your own phones. Espresso drives taps, text entry and navigation inside the app, while Device Farm reports which tests passed or failed on each device model and Android version.

What are Espresso and AWS Device Farm?

Espresso is Google's framework for writing user-interface tests for Android apps. A test describes what a user does, such as tapping a button, typing text or scrolling a list, and Espresso checks that the app responds as expected. It runs inside the app process, so it can wait for the interface to settle before checking a result, which makes tests less flaky than tests that guess timing.

AWS Device Farm is a cloud service from Amazon that runs tests on real phones and tablets hosted in its data centres. You upload your app and your tests, pick the devices to run on, and Device Farm returns a report for each device: which tests passed, which failed, screenshots, videos and logs.

The two fit together because Espresso writes the tests and Device Farm supplies the devices. Your team writes tests once, and Device Farm runs them on dozens of device models and Android versions that the team does not own.

How do the two fit together?

The pair works as a pipeline: Espresso describes the test, your build packages it, and Device Farm executes it on real hardware and reports the result.

StepWho does itWhat comes out
Write the testEspresso, in the app projectA test that taps and checks the UI
Build the packageGradle buildApp APK and test APK
Choose devicesDevice Farm device poolA set of real phones and tablets
Run the testsDevice FarmOne run per device
Read the reportDevice Farm consolePass or fail per test per device

The key point is that the same test APK runs on every device in the pool. A test that passes on a new phone and fails on an old one tells you the problem is the device or the Android version, not the test itself.

How do you write a first Espresso test?

You write a first Espresso test by picking one user flow, such as logging in, and describing each step in plain terms: find the field, type the text, tap the button, check the result.

  1. Pick one flow. Choose a short flow with a clear result, such as logging in and seeing the home screen.
  2. Find each element by its id. Give the views stable resource ids in the app, so the test does not depend on text that changes.
  3. Describe the actions. Write the steps in order: type the email, type the password, tap the sign-in button.
  4. Check the result. Assert that the home screen appears, or that the right error message shows for a wrong password.
  5. Run it locally first. Run the test on an emulator or a connected phone and fix it until it passes every time.

Keep the first test small. A test that covers one flow and passes reliably is worth more than ten tests that fail at random. Flaky tests teach the team to ignore failures, which defeats the purpose.

How do you run the tests on Device Farm?

You run the tests on Device Farm by uploading the two APKs, choosing a device pool, and starting a run. The service then executes the tests on each device in the pool.

  1. Build both APKs. Build the app APK and the test APK from the same code, so the tests match the app they check.
  2. Create a project in Device Farm. One project per app keeps runs, reports and device pools separate.
  3. Pick a device pool. Choose the devices your users actually have, mixing new and old models and Android versions.
  4. Upload and configure the run. Upload the APKs, pick the test type as Espresso, and set a timeout per test.
  5. Start the run. Device Farm queues the run and executes it on each device, in parallel where possible.
  6. Wait for the report. Each device produces its own report with pass or fail per test, plus screenshots and logs.

A run on 12 devices takes about as long as a run on one, because Device Farm runs devices in parallel. The time you spend is mostly in writing the tests and reading the failures, not in waiting for devices.

How do you read the results?

You read the results device by device. Device Farm shows a summary first: how many tests passed and failed across the run, and which devices had failures. Then you open the failing device to see what went wrong.

Each device report includes the test log, screenshots taken during the run, and a video of the session. The video is often the fastest way to see the failure: a button that is off-screen on a small phone, a dialog that appears only on one Android version, or a layout that breaks in a sideways view.

Group failures by cause, not by device. If five devices fail the same test, the cause is usually one thing, such as a missing permission on older Android versions. Fix the cause, not the device list, and re-run the same test to confirm.

What does it cost in time and money?

The time cost is mostly in writing and maintaining tests. A first Espresso test takes a few hours to write and stabilise, and a suite of twenty tests takes days rather than hours. The run itself is fast, because Device Farm runs devices in parallel, so a run on 12 devices takes roughly the time of the longest single test, not twelve times as long.

The money cost is per device-minute on Device Farm, which means you pay for the minutes each device spends running your tests. A short suite on a small pool costs little, while a long suite on a large pool adds up, so keep tests fast and the pool focused on the devices your users actually have. Check the vendor site for current pricing, since it changes.

The comparison that matters is against the alternative: buying and maintaining a shelf of phones, or shipping without testing on real devices. A cloud run is cheaper than a device lab, and a bug found before release is cheaper than a bug found by users.

Example (hypothetical): a team tests a login flow on 12 devices

Example (hypothetical): a team ships a shopping app and writes 6 Espresso tests for the login flow: valid login, wrong password, empty email, forgotten password, new account, and logout. Each test takes about 2 minutes to run on a device.

The team runs the suite on 12 devices in Device Farm, mixing new and old models. The total device time is 12 devices times 6 tests times 2 minutes, which is 144 device-minutes. Because the devices run in parallel, the wall-clock time is about 12 minutes, plus a few minutes of queueing.

The run finds one failure: on two old devices, the login button sits below the fold and the test cannot tap it. The team fixes the layout, re-runs the same 6 tests on those two devices, and the run passes. Without the cloud run, the bug would have reached users on those models.

What are the common mistakes?

The most common Espresso and Device Farm mistakes are writing flaky tests, testing on the wrong devices, and ignoring failures that only happen on one model.

  • Flaky tests. Tests that depend on timing or network speed fail at random, and the team learns to ignore them.
  • Wrong device pool. Testing only on new phones misses the old models your users actually carry.
  • Ignoring single-device failures. A failure on one model is usually a real bug on that model, not a bad test.
  • No stable ids. Tests that find views by text break whenever the wording changes.
  • Too many devices. A pool of 50 devices sounds thorough, but the bill and the failure list both grow faster than the value.

For the wider picture, mobile app testing strategies cover what to test first.

For a comparison with the alternatives, see mobile automation testing tools.

Mobile app development covers the build side.

Key takeaways

  • Espresso writes the UI tests; Device Farm supplies the real devices to run them on
  • The same test APK runs on every device in the pool, so failures point at the device, not the test
  • Runs are parallel, so 12 devices take about as long as one
  • Read failures by cause, not by device, and use the video to see what went wrong
  • Pick the device pool from your users' real devices, not from the largest list

Frequently asked questions

What is Espresso in Android testing?

Espresso is Google's framework for writing user-interface tests for Android apps. A test describes what a user does, such as tapping a button or typing text, and Espresso checks that the app responds as expected, waiting for the interface to settle before checking.

What is AWS Device Farm?

AWS Device Farm is a cloud service from Amazon that runs tests on real phones and tablets hosted in its data centres. You upload your app and tests, pick the devices, and it returns a report per device with pass or fail results, screenshots, videos and logs.

Do I need both Espresso and Device Farm?

No, but they solve different problems. Espresso writes the tests, and Device Farm runs them on real devices you do not own. You can run Espresso tests on an emulator alone, but you will not catch bugs that only appear on real hardware.

How long does a Device Farm run take?

About as long as the longest single test, because devices run in parallel. A suite of 6 tests at 2 minutes each takes roughly 12 minutes of wall-clock time on 12 devices, plus a few minutes of queueing.

How much does AWS Device Farm cost?

It charges per device-minute, so the cost depends on how many devices you run and how long the tests take. Keep tests fast and the pool focused on devices your users actually have, and check the vendor site for current pricing.

What is the most common mistake with Espresso tests?

Writing flaky tests that depend on timing or network speed. They fail at random, the team learns to ignore them, and real failures get missed. Give views stable ids, keep tests short, and fix a flaky test the day it appears.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Shipping an Android app and need a testing plan? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Mobile App Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call