Sub topic 14
Testing Flutter Widgets Under a Specific Locale
This chapter assumes you know basic Dart test syntax, can build widgets with MaterialApp, and have already generated AppLocalizations from ARB files. The examples use Spanish (es) and a package named trip_planner. Replace that package name with your app's package name.
A localization widget test should control the locale instead of depending on the computer or test device language. Set locale on MaterialApp, then provide the same localization delegates and supported locales used by the application.
Building a localized test harness
The following test renders one localized message. pumpWidget places the widget tree in the test environment. pumpAndSettle waits for scheduled frames to finish. The test then searches for the translated text.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:trip_planner/l10n/app_localizations.dart';
void main() {
testWidgets('shows the Spanish greeting', (tester) async {
await tester.pumpWidget(
MaterialApp(
locale: const Locale('es'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) {
final l10n = AppLocalizations.of(context)!;
return Text(l10n.greeting('Ada'));
},
),
),
);
await tester.pumpAndSettle();
expect(find.text('¡Hola, Ada!'), findsOneWidget);
});
}The Builder creates a context below MaterialApp. That placement matters because AppLocalizations.of(context) searches the widget tree for the localization data installed by MaterialApp.
You can test a plural message by passing a particular count. This verifies both locale selection and ICU plural selection.
testWidgets('shows the Spanish plural for one trip', (tester) async {
await tester.pumpWidget(
MaterialApp(
locale: const Locale('es'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) {
final l10n = AppLocalizations.of(context)!;
return Text(l10n.tripCount(1));
},
),
),
);
await tester.pumpAndSettle();
expect(find.text('1 viaje'), findsOneWidget);
});Testing more than one locale
Use a separate test for each important supported locale. A test that passes in English does not prove that the Spanish ARB file contains every message or that its placeholders are correct.
Test the fallback behavior separately when it matters. For example, Locale('es', 'MX') may resolve to the supported es locale when only general Spanish is supported. Do not assume that every regional locale has a matching ARB file.
Common test failures
AppLocalizations.of(context)returnsnullwhen the context is aboveMaterialAppor the delegates were omitted.- The test finds no text when the expected translation does not match the ARB value exactly.
- A test accidentally uses the device locale when
localeis not set. - A missing placeholder or malformed ARB message can prevent generated localization code from updating. Run
flutter gen-l10norflutter pub getafter changing ARB files. - Testing only English leaves translated messages and plural branches unchecked.
Exercise
Why should a localization widget test set both locale and localizationsDelegates on MaterialApp?
Show answer
locale selects the language being tested, while localizationsDelegates loads the generated and framework localization data that makes translated messages available to widgets.