Flutter testing
Skill almasumdev/awesome-flutter-agent-skills/.github/skills/testing_and_automation/flutter-testing
Setup and patterns for Unit, Widget, and Golden testing in Flutter using flutter_test, mocktail, and alchemist/golden_toolkit. Use this when writing or reviewing tests.From its SKILL.md
npx -y skills add almasumdev/awesome-flutter-agent-skills --skill flutter-testingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 1 stars1 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
3.7 KB, 858 tokens by cl100k_base, as published. Nobody here has run it
Flutter Testing — Unit, Widget, Golden
Instructions
Every feature should have tests at three levels: unit (logic), widget (rendering + interaction), and — for critical UI — golden (pixel regression).
1. Unit Tests
- Use
package:test/package:flutter_test(both exporttest,expect,group). - Mock collaborators with mocktail (preferred — no code-gen) or mockito.
class MockArticleRepo extends Mock implements ArticleRepository {}
void main() {
late ArticlesNotifier sut;
late MockArticleRepo repo;
setUp(() {
repo = MockArticleRepo();
when(() => repo.getAll()).thenAnswer((_) async => [fakeArticle]);
});
test('loads articles on build', () async {
final container = ProviderContainer(overrides: [
articleRepoProvider.overrideWithValue(repo),
]);
addTearDown(container.dispose);
final value = await container.read(articlesProvider.future);
expect(value, [fakeArticle]);
});
}
For BLoC use package:bloc_test:
blocTest<ArticlesCubit, ArticlesState>(
'emits [loading, data] on load',
build: () => ArticlesCubit(repo),
act: (c) => c.load(),
expect: () => [const ArticlesState.loading(), ArticlesState.data([fakeArticle])],
);
2. Widget Tests
Use testWidgets with WidgetTester:
testWidgets('shows articles', (tester) async {
await tester.pumpWidget(ProviderScope(
overrides: [articleRepoProvider.overrideWithValue(repo)],
child: const MaterialApp(home: ArticlesScreen()),
));
await tester.pumpAndSettle();
expect(find.text(fakeArticle.title), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('article-${fakeArticle.id}')));
await tester.pumpAndSettle();
// assert navigation result
});
Guidelines:
- Always provide a
MaterialApp(orMediaQuery+Directionality) so widgets have required inherited widgets. - Use
find.byKey(...)with stable keys overfind.text(...)for robustness against copy changes. - Use
tester.pumpAndSettle()sparingly — it hides timing bugs. Prefer explicitpump(Duration).
3. Golden Tests
For pixel-accurate regressions, use alchemist (preferred, ignores font/platform differences) or golden_toolkit.
goldenTest(
'ArticleCard renders variants',
fileName: 'article_card',
builder: () => GoldenTestGroup(
children: [
GoldenTestScenario(name: 'default', child: ArticleCard(article: fakeArticle)),
GoldenTestScenario(name: 'long title', child: ArticleCard(article: longTitleArticle)),
],
),
);
- Commit goldens to the repo. Regenerate with
flutter test --update-goldensonly when the UI intentionally changed. - Run goldens on a single canonical platform (CI Linux) to avoid per-OS flakiness.
4. Semantics / A11y in Tests
final handle = tester.ensureSemantics();
expect(find.bySemanticsLabel('Add to favorites'), findsOneWidget);
handle.dispose();
5. Test Hygiene
- Each test is independent (
setUprebuilds fresh state; no shared mutable globals). - Fakes/mocks are reset between tests.
-
disposeverified for controllers and subscriptions. - CI runs
flutter test --coverageand publishes the report. - Goldens run only on a pinned CI image.
6. Running
- Unit + widget:
flutter test. - Coverage:
flutter test --coverage→coverage/lcov.info. - Update goldens:
flutter test --update-goldens. - Single file:
flutter test test/features/articles/articles_notifier_test.dart.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.