Running Espresso and XCUITest Suites in the Cloud
On this page
Espresso and XCUITest tests are written by app developers, live in the app's repository and are compiled together with the app. Running them on a device cloud works differently from Appium: you do not open a session and send commands. You upload two binaries, the app and the test bundle, and the platform's own test runner executes the whole suite on the device. This chapter runs an Espresso suite on TestingBot and explains the XCUITest equivalent.
What an Espresso test looks like#
This is one of the tests in TestingBot's Espresso demo app, from CalculatorTest.java:
@Test
public void ensureAdditionWorks() {
takeActivityScreenshot("addition_works");
// Type text and then press the button.
onView(withId(R.id.but3)).perform(click());
onView(withId(R.id.butplus)).perform(click());
onView(withId(R.id.but5)).perform(click());
onView(withId(R.id.buteq)).perform(click());
// Check that the text was changed.
onView(withId(R.id.output)).check(matches(withText("8.0")));
}
Compare it with the Appium test from chapter 4. There are no waits at all: Espresso runs inside the app's process, sees when the main thread is idle and only then performs the next action. Views are found by their generated resource ids (R.id.but3), which the compiler checks. The price is that this test cannot exist without the app's source code, and it only runs on Android.
The two files an Espresso run needs#
Gradle builds them from the same project:
./gradlew assembleDebug assembleDebugAndroidTest
That produces app/build/outputs/apk/debug/app-debug.apk (the app) and app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk (the tests plus the instrumentation runner). The demo project pins an older Gradle (6.5), which predates current JDKs: this course built it with JDK 11. The demo app's 1.0.0 release has both prebuilt, which is what this chapter uses.
Run the suite#
npx @testingbot/cli@1.5.0 espresso app-debug.apk app-debug-androidTest.apk \
--device "Pixel 8" --platform-version "14" \
--name "Calculator Espresso" --build "course-mobile-app-testing"
The CLI uploads both APKs, starts the run and streams the instrumentation output. On a Pixel 8 emulator with Android 14 the demo suite ran six tests: five passed and AdvancedCalculatorTest.ensureLargeNumbersWork failed (session 894e8d77c9a3-...). That failure is known and deliberate in the 1.0.0 release: the test expects the wrong text. Chapter 8 uses it to practise reading a failed run.
To run part of a suite, the CLI passes the standard instrumentation filters through: --class, --package, --annotation and --size, plus --not- variants of each. Running only the calculator class:
npx @testingbot/cli@1.5.0 espresso app-debug.apk app-debug-androidTest.apk \
--device "Pixel 8" --platform-version "14" \
--class com.testingbot.calculator.CalculatorTest \
--name "Calculator Espresso" --build "course-mobile-app-testing" \
--report junit --report-output-dir ./reports
com.testingbot.calculator.CalculatorTest:
Time: 11.115
OK (5 tests)
Run 3255 (Pixel 8): Test completed successfully
All tests completed successfully!
Saved report: reports/espresso_report_3234.xml
That run (session 76d9e25dbe51-..., Pixel 8 emulator, Android 14) passed and saved a JUnit XML report that a CI system can display. For a physical phone, add --real-device and pick a real device name, for example --device "Galaxy S24" --real-device.
A developer changes a button label and wants to re-run the Espresso tests in the cloud. What has to be rebuilt and uploaded?
The label lives in the app, so the app APK must be rebuilt. If a test assertion was updated to match, the test APK changes as well. Both are compiled artefacts; nothing is fetched from source at run time.
XCUITest: the same idea for iOS#
An XCUITest run on TestingBot also takes two files, as the XCUITest documentation describes:
- the app as a signed
.ipa, and - the tests as a
.zipof the-Runner.appthat Xcode produces with Product > Build For > Testing (orxcodebuild build-for-testing). Build an XCUITest suite walks through finding and zipping it.
The documented CLI command has the same shape as Espresso's: testingbot xcuitest app.ipa app-test.zip --device "iPhone 16" --real-device.
This course did not run an XCUITest suite: the XCUITest sample (xcuitest-example-app) is source code only, and turning it into an .ipa needs an Apple developer signing identity. The command is the form the documentation gives, not a run we can show you a result for. Everything else carries over from Espresso: filters for which tests to run, --build and --name, --report junit, --retry, and artifacts after the run.
You zip the folder Build/Products/Debug-iphonesimulator and upload it as the XCUITest suite for a real iPhone. The run fails to install the tests. Why?
Simulator products cannot be installed on hardware, for tests as much as for apps. Build for testing against a generic iOS device and zip the -Runner.app from Debug-iphoneos.
Native suites next to cross-platform ones#
Running Espresso and XCUITest in the cloud gives developers what their own machines cannot: many OS versions and real hardware without a device drawer. It does not replace a cross-platform Appium or Maestro suite, because each native suite only covers one platform and is written by the people who wrote the code. Many teams run both: native suites on every pull request, a smaller end-to-end suite on both platforms before release. The Espresso documentation lists every option, including retries, timeouts, queue priority and the Gradle plugin that uploads and runs straight from your build.