Bringing localization into your Widget testing

Search for a command to run...

No comments yet. Be the first to comment.
Here we will cover various aspects of unit, widget and integration testing in Flutter
In this post, we going to go through how, at least in my opinion, one would go about testing that a function is called within a widget when that function is one of the Widgets arguments. Take the following overly simplified example… class SampleWidge...
The Problem Wasn’t Triage Most AI workflows in software engineering still keep the human directly in the middle of triage. The AI might help write code. It might explain a stack trace. It might summar

There’s been a steady undercurrent of people switching tools lately. Copilot to Claude. Claude to something else. Codex quietly entering more workflows. A lot of confidence in different directions, of

It’s been said to me that at times my responses can come across as a “no” first-unintentionally, of course, but real all the same. Not long ago, someone on my team shared an idea they were exploring. I began with a caution, meaning to help them avoid...

The opening sentence of any message often carries more weight than everything that follows. I’ve learned this the hard way. Recently, a report shared something he wanted to pursue. I was supportive — I really was — but my first response came as a cau...

One of the things I’ve always been comfortable with is admitting when I don’t know something. To me, it’s one of the real distinctions between the more and less experienced engineers — and honestly, people in general. When you’re earlier in your care...

The more accurate you make your test, the higher the quality of the test itself.
Many apps these days are built to be more accessible, one thing we do to ensure this is we localize our applications, allowing people who do not speak the same language as we do, to also use the application.
In our application, we have been using easy_localization, I know there are quite a few choices, but for us, this was one of the easier ones to implement and for us, the JSON support was a big win with how we manage our localization.
To ensure we have the most accurate widget tests, we rather use “real” localizations than having to mock the helper classes.
To do this we keep a local copy of the locale JSON files to keep them safe from external updates and have created a helper function createLocalizedWidgetFortesting which looks like:
Widget createLocalizedWidgetForTesting({Widget child}) {
return EasyLocalization(
path: '$TEST_MOCK_STORAGE/locale',
useOnlyLangCode: true,
assetLoader: FileAssetLoader(),
fallbackLocale: const Locale('en'),
supportedLocales: globals.supportedLocale,
saveLocale: false,
child: MaterialApp(
home: Scaffold(
body: child,
),
),
);
}
As you will see, EasyLocatlizationrequires a path, that is simply a constant that we have defined a little higher in the file, as it is used as part of other mocks.
const TEST_MOCK_STORAGE = './test/fixtures/core';
Beyond that, everything else is pretty much how it is set up in main.dart .
Here is a simplified example of how a test would look with this implementation.
void main() {
testWidgets(
'Should render localized widget',
(
WidgetTester tester,
) async {
await tester.pumpWidget(
createLocalizedWidgetForTesting(
child: SampleWidget(),
),
);
await tester.pumpAndSettle();
// expectations to follow
}
);
}
Through this would be able to run proper expectations like:
expect(find.text("Sample localized text", findsOneWidget);
More importantly, if you have a language switcher that a user can select you can test for text changes through a setup flow.
I hope you found this interesting, and if you have any questions, comments, or improvements, feel free to drop a comment. Enjoy your Flutter development journey :D
If you liked it, a like would be awesome, and if you really liked it, a cup of coffee would be great.
Thanks for reading.
If you wish to carry on with the subject of testing, why not take a look at: