Expert · Responsive design and testing · topic 42 of 50 · 30 min
Integration tests and CI
Explanation
The testing pyramid
Fast unit tests form the base (many), widget tests the middle (some), and integration tests the top (a few). Integration tests are slower and more fragile because they start the real app on a device, but they are the only ones that prove a full journey works end to end: launch, log in, add to cart, pay.
/\ integration tests: few, slow, whole app on a device
/ \
/----\ widget tests: some, fast, one screen at a time
/ \
/--------\ unit tests: many, very fast, pure logicWriting an integration test
# pubspec.yaml
dev_dependencies:
integration_test:
sdk: flutter
flutter_test:
sdk: flutter// integration_test/order_flow_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:chai_shop/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('user can add chai and see it in the cart', (tester) async {
app.main(); // start the real app
await tester.pumpAndSettle();
await tester.tap(find.text('Masala chai'));
await tester.pumpAndSettle();
await tester.tap(find.text('Add to cart'));
await tester.tap(find.byIcon(Icons.shopping_cart));
await tester.pumpAndSettle();
expect(find.text('Masala chai x1'), findsOneWidget);
});
}flutter test integration_test # on the connected device or emulator
flutter test integration_test/order_flow_test.dartGolden tests are another tool: they render a widget and compare it pixel by pixel with a saved reference image, catching accidental visual changes. They are useful for design systems but can be sensitive to fonts and platforms.
Choose integration tests for the journeys that make or lose money and trust: sign-up and login, search to checkout, booking and payment. Keep each test independent, start from a known state (for example a fresh test account or seeded test data), and avoid depending on exact timings; use finders and pumpAndSettle instead of fixed waits.
Integration tests can also run on cloud device farms such as Firebase Test Lab, which run your tests on many real phone models. This is useful for catching problems that appear only on certain manufacturers or Android versions.
Static analysis and formatting
flutter analyze # finds errors, warnings and lint issues without running the app
dart format . # formats all Dart files in the standard style
dart format --output=none --set-exit-if-changed . # CI check: fail if anything is unformattedFix analyzer warnings as they appear; a clean analyzer is a good team habit.
CI: tests on every push
Continuous integration (CI) means a server automatically checks every change. With GitHub Actions, a small YAML file runs analysis and tests whenever someone pushes or opens a pull request; a failing check blocks broken code from being merged.
name: flutter-ci
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2 # a widely used community action to install Flutter
with:
channel: stable
- run: flutter pub get
- run: dart format --output=none --set-exit-if-changed .
- run: flutter analyze
- run: flutter testCommon mistakes
- Trying to cover everything with integration tests: slow and flaky suites that nobody runs.
- Integration tests that depend on live production servers or real payments.
- Ignoring analyzer warnings until there are hundreds.
- No CI, so tests are only run "when someone remembers".
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
Integration tests and CI · 1/6
The testing pyramid: many unit tests, some widget tests, a few integration tests.
Prefer a video by a teacher? Find free videos on “flutter integration test integration_test github actions ci flutter analyze” on YouTube ↗
In real life: A safety net for a growing team
A three-person team building a tuition-booking app adds CI: every pull request runs dart format check, flutter analyze and 180 unit and widget tests in about four minutes. Two integration tests (sign up, book a class) run nightly on an emulator. When a new developer accidentally breaks the booking time calculation, CI fails on their pull request before it reaches users.
Practice (0/3 done)
Which test type is slowest but checks a complete user journey on a device?
Which command checks code for errors and lint issues without running the app?
Add one integration test that launches your app, logs in with a test account (or skips login with a fake), performs one key action and checks the result. Then add a GitHub Actions workflow that runs dart format check, flutter analyze and flutter test on every push, and push a commit to see it run.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Unit testsResponsive design and testing · same module
- Responsive and adaptive designResponsive design and testing · same module
- Integration testing: checking parts work togetherBackend Development · Testing and API documentation
- Testing a full-stack appFull-Stack Development · Performance, security and testing
- Testing basics: unit, integration and end-to-endFrontend Web Development · Fast, accessible, findable and safe: quality for real sites
- Testing basics and the test pyramidKotlin and Android App Development · Testing Android apps
Questions people ask about this
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 binding must integration tests initialise?
Answer: IntegrationTestWidgetsFlutterBinding
Call ensureInitialized() first.
What does dart format --set-exit-if-changed do in CI?
Answer: Fails the job if any file is not formatted
Enforces consistent style.
What should integration tests use for login and payments?
Answer: Test accounts and a test or fake backend
Never use real money or data.
What is CI?
Answer: Automatic checks (analysis, tests) on every code change
Continuous integration.
What does an integration test check that a unit test does not?Related
Answer: That several parts, like handler and database, work together
Integration tests check the joins between parts, where many real bugs live.
What does an integration test of POST /api/notes usually use?Related
Answer: The real handler and a separate test database
Integration tests check that the pieces work together (route, validation, SQL) but against a throwaway test database.
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.
What does the test pyramid recommend?Related
Answer: Mostly unit tests, fewer integration tests, few UI tests
Fast, focused tests form the base.
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
- This is the last topic of “Responsive design and testing”. The module exam comes next.
- Module 10 exam: Responsive design and testing · 12 questions · 25 min, without help. 4 topics of this module still to finish first.
- Then the next module, Publishing to the stores and the web. 50 topics left in the course before the final exam.