Back to Articles

Intermediate

Writing Widget and Integration Tests in Flutter

· 8 min read

The Three-Layer Test Pyramid

In Flutter, tests are thought of in three layers: unit tests (test the logic of a single function or class, no UI), widget tests (build a widget in a fake environment and verify its behavior — there's UI but no real device), and integration tests (run the whole app end to end on a real device or emulator). All three use the flutter_test package; integration tests additionally need the integration_test package.

A Basic Widget Test

testWidgets is a special test function that injects a WidgetTester. You build the widget tree with tester.pumpWidget, find widgets with find, and verify with expect.

testWidgets('sayac butona basinca artar', (tester) async {
  await tester.pumpWidget(const MaterialApp(home: SayacSayfasi()));

  expect(find.text('0'), findsOneWidget);

  await tester.tap(find.byIcon(Icons.add));
  await tester.pump();

  expect(find.text('1'), findsOneWidget);
});

Finding Widgets: find

find.byType searches by widget class, find.byKey by a Key you've explicitly provided, find.text by visible text, and find.byIcon by icon. Using a Key is the most reliable approach for widgets that aren't otherwise unique:

find.byType(ElevatedButton);
find.byKey(const Key('giris-butonu'));
find.text('Giriş Yap');
find.byIcon(Icons.search);

Interaction: tap, enterText, pump

tester.tap simulates a tap, tester.enterText types text into a TextField. You need to call tester.pump() after every interaction — this tells Flutter to render the next frame; for animated transitions, pumpAndSettle() waits until all animations finish.

testWidgets('form gonderilince hata gosterir', (tester) async {
  await tester.pumpWidget(const MaterialApp(home: GirisFormu()));

  await tester.enterText(find.byKey(const Key('email-alani')), 'gecersiz');
  await tester.tap(find.byKey(const Key('gonder-butonu')));
  await tester.pumpAndSettle();

  expect(find.text('Geçerli bir e-posta girin'), findsOneWidget);
});

Golden Tests: Visual Regression

matchesGoldenFile compares a widget's rendered output against a reference PNG, pixel by pixel. You generate the reference file on the first run with the --update-goldens flag; on every later run, the test fails if there's a visual difference. This is handy for visually critical widgets like cards and buttons in a design system, but needs careful CI setup because of cross-platform font differences.

End-to-End Testing with integration_test

Widget tests run in a fake environment (no real platform code, camera, or network). The integration_test package instead runs the test on a real device or emulator, launching the full app — so real navigation, real Firebase calls and similar behavior can also be verified. To run it:

flutter test integration_test/uygulama_akisi_test.dart

Things to Watch Out For

1. Forgetting pump()

If you don't call pump() after an interaction that triggers setState, the widget tree stays in its old state and your expect call mistakenly verifies the stale value. Make pump() or pumpAndSettle() after every interaction a habit.

2. Tests That Depend on Real Network/Firebase Calls

Widget tests shouldn't depend on external services; otherwise they become slow, flaky, and impossible to run offline. Replace services with mock/fake implementations and hand them to the widget through dependency injection.

Conclusion

Widget tests run fast and reliably verify UI logic — what happens when a button is tapped, what's shown when a form gets bad input. Target elements with find, never forget pump() after an interaction, use golden tests for visually critical components, and integration_test for real-device behavior.