Sub topic 12
Running on CI and Cloud Device Farms
This chapter assumes you can write a Flutter widget test, use integration_test, run Flutter commands in a terminal, and identify widgets with stable keys. You also need a CI service, such as GitHub Actions, and access to an Android or iOS device target.
Local device testing answers, “Does this user flow work on this device?” Continuous integration (CI) repeats that check whenever code changes. A cloud device farm runs the same test on hosted physical devices or emulators with different operating systems, screen sizes, and versions.
What CI must provide
An integration test needs more than the Dart test runner. The CI job must install Flutter, obtain dependencies, build the application, start or connect to a device, run the test, and save useful results.
A typical pipeline has this order:
Keep the unit and widget tests in an earlier job. They run quickly and give better feedback for small code errors. Run integration tests after those checks pass because device tests take longer and need more infrastructure.
A minimal CI command can look like this:
#!/usr/bin/env bash
set -euo pipefail
flutter pub get
flutter analyze
flutter test test
flutter test integration_test/app_test.dart -d "$FLUTTER_DEVICE_ID"The job must define FLUTTER_DEVICE_ID. On a self-hosted runner, it might be an emulator identifier. On a runner supplied by a device-farm provider, the provider usually exposes a different command or test package format.
Build artifacts and test results
A cloud device farm normally cannot read your local Flutter project. The CI job must create an artifact, which is a file produced for another system to consume.
For Android, the artifact might be an APK. For iOS, it is commonly an application bundle packaged for the provider. The exact build command depends on whether the test runs in debug, profile, or release mode and on the provider's requirements.
Save these items when a test fails:
- Test output and exit status.
- Device logs.
- Screenshots or video supplied by the device farm.
- Flutter performance timelines, when the test uses
traceAction. - The application and test artifacts, when debugging the build itself.
Use CI secrets for signing credentials, provider tokens, and certificates. Do not place these values in pubspec.yaml, source files, or printed command arguments.
Choosing devices
A device matrix is a list of device and operating-system combinations. Start with a small matrix that represents your users. Add more combinations when analytics or bug reports justify the cost.
| Test target | Best use | Main cost |
|---|---|---|
| Local emulator | Fast development feedback | Does not represent every physical device |
| CI emulator | Repeatable smoke tests | Setup and maintenance time |
| Physical device farm | Hardware and OS coverage | Usage fees and queue time |
| Self-hosted device | Specialized hardware testing | Device maintenance and availability |
Use a short smoke suite on every pull request. Run a broader matrix on the main branch or on a scheduled workflow. Tests that depend on camera hardware, Bluetooth, push notifications, or platform-specific permissions often need real devices.
Cloud device farm workflow
Each provider has its own upload and execution commands, but the process is usually similar:
- Build the application and integration-test package.
- Upload the artifacts to the provider.
- Choose device models and operating-system versions.
- Configure permissions, network access, and test data.
- Start the test run and wait for its status.
- Download logs, screenshots, and videos.
- Fail the CI job when the provider reports a test failure.
Keep provider-specific commands in one CI script. The test code should continue to use Flutter APIs such as find, tap, enterText, and pumpAndSettle. This separation makes it easier to change providers.
Keeping farm runs reliable
Cloud devices are slower and less predictable than a development emulator. Avoid fixed delays such as Future.delayed(const Duration(seconds: 3)). Wait for a known UI state, use pumpAndSettle when animations can finish, and provide explicit timeouts for network-backed operations.
Use controlled accounts and test data. Each test should reset its state or use a unique account. Do not make one test depend on data created by another test. Give important controls stable keys, because text and layout can change across locales and device sizes.
When a farm test fails, first decide whether the failure is in the application, the test, or the infrastructure. Compare the device log, screenshot, and test output. Retry infrastructure failures only when the provider identifies them as transient. Blindly retrying assertion failures can hide real regressions.
Exercise
A test passes locally but fails intermittently on a cloud device. What should you inspect first?
Show answer
Inspect the failure screenshot and device log, then look for fixed delays, shared test data, missing stable keys, unfinished animations, and network-dependent state. Replace timing assumptions with explicit state checks before adding retries.