Sub topic 12
Running on CI and cloud device farms
This chapter moves your integration tests off your desk and into automation. It assumes you can already write and run an integration test with flutter test integration_test -d <device_id>, that you know what flutter drive does, and that you have used a basic YAML-based CI service such as GitHub Actions. You do not need to know Gradle or Xcode internals.
Why run integration tests on CI
A test that only runs on one developer's phone protects very little. Running the suite automatically on each pull request gives you three things:
- Regressions are caught before merge, while the change is still small.
- The test environment is the same every run, which removes "works on my machine".
- Results are recorded, so you can compare a failing run with the last passing one.
Integration tests are slower and more expensive than unit or widget tests. Most teams run unit and widget tests on every push and integration tests on pull requests, on merge to the main branch, or nightly.
The three places a test can run
CI gives you three kinds of target. Each one trades realism against cost and speed.
| Target | Realism | Speed | Cost | Typical use |
|---|---|---|---|---|
| Emulator or simulator on a CI runner | Medium: no real sensors, vendor skins or GPU quirks | Slow to boot, then fast | Runner minutes only | Every pull request |
| Desktop or web on a CI runner | Low for mobile behavior | Fastest | Runner minutes only | Smoke tests for shared UI logic |
| Cloud device farm with physical devices | High | Queue time plus upload | Per-device-minute pricing | Release candidates, device-specific bugs |
Preparing the project for unattended runs
CI has no human to fix a failure by hand, so check these points before writing any pipeline.
- Commit
pubspec.lock. Pin the Flutter version in CI. An unpinnedstablechannel can change under you between two runs. - Make the app start without manual steps. No login you type by hand, no permission dialog you tap yourself. Use test accounts, seeded data, or a fake backend that your test configures.
- Give every widget the test touches a stable
Key. Finders based on text break when copy changes. - Keep the suite in
integration_test/. Runningflutter test integration_testthen picks up every*_test.dartfile there.
The determinism rules from the earlier chapters matter more here. A test that fails once in twenty runs on your laptop will fail on CI weekly, because CI runners are slower and more variable.
Android emulator on GitHub Actions
The reactivecircus/android-emulator-runner action boots an emulator, runs a script you supply, and shuts the emulator down. Hardware acceleration (KVM) must be enabled on Linux runners, otherwise the emulator is too slow to be useful.
name: integration-tests
on:
pull_request:
jobs:
android:
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- uses: subosito/flutter-action@v2
with:
channel: stable
cache: true
- run: flutter pub get
- name: Enable KVM
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: Run integration tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 34
arch: x86_64
profile: pixel_6
script: flutter test integration_test -d emulator-5554Points worth noting:
timeout-minutesstops a hung emulator from consuming hours of runner time.emulator-5554is the id the first emulator normally receives. If you are unsure, addflutter devicesto the script and read the id from the log.- The first run downloads a system image. The action can cache an AVD snapshot to speed up later runs, which is worth setting up once the suite grows.
iOS simulator on a macOS runner
iOS tests need macOS because the simulator is part of Xcode. macOS runners cost more per minute than Linux runners on most CI services, so many teams run iOS only on merges to main.
ios:
runs-on: macos-latest
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
with:
channel: stable
cache: true
- run: flutter pub get
- name: Start iOS simulator
uses: futureware-tech/simulator-action@v3
with:
model: 'iPhone 15'
- name: Run integration tests
run: flutter test integration_testWith one simulator booted and no other devices connected, flutter test selects it automatically. If more than one device is present, pass -d <device_id>, using an id from flutter devices.
Desktop and web targets
Desktop and web runs are the cheapest to automate, and they exercise the same widget code.
For Linux desktop, install the build dependencies and provide a virtual display, because the app opens a real window:
sudo apt-get update
sudo apt-get install -y clang cmake ninja-build pkg-config libgtk-3-dev xvfb
xvfb-run flutter test integration_test -d linuxFor web, flutter test does not run integration tests in a browser. Use flutter drive with a ChromeDriver process, as described in the earlier chapter on the driver:
chromedriver --port=4444 &
flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
-d web-serverThe web-server device serves the app without opening a visible browser window, so it suits headless CI. The ChromeDriver version must match the installed Chrome version, and mismatches are a common cause of web CI failures.
Cloud device farms
An emulator is not a phone. Real devices differ in GPU drivers, memory limits, manufacturer-modified Android versions, and screen sizes. Device farms such as Firebase Test Lab, AWS Device Farm and BrowserStack App Automate let you run your tests on racks of physical devices you do not own.
The workflow is the same on every farm:
- Build your app and its test harness as installable binaries.
- Upload them to the farm and choose a device list.
- Wait for the farm to run the tests and collect logs, videos and results.
- Download the results and fail the CI job if the farm reports a failure.
The key point is that a farm does not run flutter test. It expects a native test package, because it only knows how to run native test runners. For Android that is an instrumentation test APK. For iOS it is an XCTest bundle. Flutter's integration_test package supports this by wrapping your Dart tests in a native runner.
Firebase Test Lab on Android
Android needs a one-time native setup in your project. You add an instrumentation test class under android/app/src/androidTest/ that launches the Flutter test runner, and you add the AndroidX test dependencies to android/app/build.gradle. The exact class and dependency versions change between releases of the package, so copy them from the integration_test package README for your Flutter version rather than from an old blog post.
After that, you build two APKs: the app, configured to run your Dart test file as its entry point, and the instrumentation test package. Then you submit both to Test Lab with the gcloud command line tool.
pushd android
./gradlew app:assembleAndroidTest
./gradlew app:assembleDebug -Ptarget=integration_test/app_test.dart
popd
gcloud firebase test android run \
--type instrumentation \
--app build/app/outputs/apk/debug/app-debug.apk \
--test build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk \
--device model=pixel6,version=33,locale=en,orientation=portrait \
--timeout 5m-Ptarget=... tells the Flutter Gradle plugin which Dart file to build as the app's entry point. Because of this, one build contains one test file. To run several files, either combine them into a single entry file that calls each file's main, or build and submit once per file.
Firebase Test Lab on iOS
iOS follows the same idea with different tools. You build the app for a physical device with xcodebuild build-for-testing, which produces an .xctestrun file and the built products. You zip those and submit the archive with gcloud firebase test ios run. Test Lab devices are physical iPhones and iPads, so the build must be signed for devices. The current commands and flags are in the Flutter and Firebase documentation. They change with Xcode versions, so check them against your toolchain.
Authentication from CI
The CI job needs permission to call the farm. Avoid long-lived keys stored as secrets where you can.
- On GitHub Actions, use workload identity federation with the
google-github-actions/authaction. It gives the job short-lived credentials. - If you must use a service account key, store it as an encrypted secret, scope the service account to Test Lab and the results bucket only, and rotate it.
- Never write credentials into the repository or print them in logs.
Collecting results and handling failures
A red CI job with no evidence is hard to debug. Plan for output from the start.
- Upload the test log, any screenshots, and performance timelines as build artifacts, and keep the upload step running even when tests fail (
if: always()on GitHub Actions). - If you use
flutter drivewithintegrationDriver, results and reports are written by the driver. Collect that output directory. - For farm runs, download the results bucket contents. Videos and device logs there often explain failures that the Dart log cannot.
- name: Upload test artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: integration-test-output
path: build/
if-no-files-found: ignoreRetries and flaky tests
Retrying a failed job hides flakiness instead of fixing it. A reasonable policy:
- Fix the root cause when you can: a missing
pumpAndSettle, a timing assumption, or shared state between tests. - If a retry is unavoidable, retry only the failing test file, once, and record every retry so the flaky test gets tracked and fixed.
- Treat a test that needs retries regularly as a bug, not as a normal cost.
Keeping cost under control
Emulator minutes and device-minutes add up quickly.
- Run integration tests on pull requests only when relevant paths change, and run the full farm matrix nightly or on release branches.
- Pick a small, deliberate device matrix: the oldest Android version you support, a current one, one small screen, one large screen, and the current and oldest supported iOS versions.
- Set a timeout on every step so a hung run ends by itself.
- Cache the Flutter SDK, pub packages and Gradle files between runs.
Common mistakes
- Starting tests before the emulator is ready. Use the action that waits for boot. If you script it yourself, wait for
adb shell getprop sys.boot_completedto return1. - Forgetting a device id when several devices exist.
flutter testfails or picks the wrong one. Always pass-din scripts that may see multiple devices. - Testing a debug build and assuming release behavior. Debug builds are slower and include assertions. Do not use debug timings as performance results. Profile on a physical device in profile mode.
- Sharing state between tests. A previous test's logged-in session or saved preferences can make the next test pass or fail unpredictably. Reset the app state in
setUp, or use a clean install per file on the farm. - Hard-coding real backend calls. A third-party outage then fails your pipeline. Point the app at a staging or fake backend for tests.
Exercise
Your team wants every pull request to run integration tests, but the budget for real-device minutes is small. Describe a pipeline that balances cost and realism, and name one thing each stage should upload when it fails.
Show answer
Run unit and widget tests first, since they are cheapest and catch most errors. Then run integration tests on an Android emulator on a Linux runner (with KVM enabled) for every pull request, and on an iOS simulator on a macOS runner only for merges to main. Send a signed release-candidate build to a device farm such as Firebase Test Lab only on release branches or nightly, using a small matrix such as the oldest and newest supported OS versions. On failure, the unit and widget stage uploads the test log, the emulator and simulator stages upload the log and any screenshots or timelines, and the device-farm stage downloads the results bucket (videos and device logs) as artifacts. Every step has a timeout, and upload steps run with if: always().
Exercise
A test passes on your laptop but is skipped by Firebase Test Lab when you submit your debug APK as both --app and --test. What is the likely cause, and what do you build instead?
Show answer
Test Lab needs a separate instrumentation test APK in addition to the app APK. Submitting the app APK as --test gives it no native test runner to execute. Build the test package with ./gradlew app:assembleAndroidTest (output under build/app/outputs/apk/androidTest/debug/), build the app with ./gradlew app:assembleDebug -Ptarget=integration_test/app_test.dart, and pass them as --test and --app respectively. Also confirm that the native androidTest runner class and dependencies from the integration_test README are in place.