Write widget tests and integration tests for Flutter
✓Works with OpenClaudeYou are a Flutter testing specialist. The user wants to write widget tests and integration tests for Flutter applications to verify UI behavior and user interactions.
What to check first
- Run
flutter test --helpto confirm the Flutter testing framework is available - Verify
pubspec.yamlincludesflutter_testdependency underdev_dependencies - Check that test files exist in the
test/directory (widget tests) orintegration_test/directory (integration tests)
Steps
- Create a widget test file in
test/directory with_test.dartsuffix (e.g.,test/home_page_test.dart) - Import
flutter_testand the widget you want to test at the top of the file - Wrap your test in a
testWidgets()function which provides aWidgetTesterinstance - Use
WidgetTester.pumpWidget()to render your widget into the test environment - Use finders like
find.byType(),find.byKey(), orfind.text()to locate widgets - Use matchers like
expect()to assert the widget state matches expected values - Simulate user interactions with
tester.tap(),tester.enterText(), ortester.drag() - Call
await tester.pumpAndSettle()to wait for animations and rebuilds to complete - For integration tests, create files in
integration_test/and usetestWidgets()with device-level interactions
Code
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('Counter Widget Tests', () {
testWidgets('Counter increments when button is tapped', (WidgetTester tester) async {
// Build the widget
await tester.pumpWidget(
const MaterialApp(
home: CounterPage(),
),
);
// Verify initial state
expect(find.text('0'), findsOneWidget);
expect(find.byIcon(Icons.add), findsOneWidget);
// Tap the increment button
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify counter incremented
expect(find.text('1'), findsOneWidget);
expect(find.text('0'), findsNothing);
});
testWidgets('Multiple taps increment correctly', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: CounterPage(),
),
);
// Tap 5 times
for (int i = 0; i < 5; i++) {
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
}
expect(find.text('5'), findsOneWidget
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Flutter Skills
Other Claude Code skills in the same category — free to download.
Flutter Widget
Build custom Flutter widgets with state management
Flutter Riverpod
Set up Riverpod for state management in Flutter
Flutter Navigation
Configure GoRouter for declarative navigation
Flutter HTTP
Build HTTP client with Dio and interceptors
Flutter Firebase
Integrate Firebase (Auth, Firestore, Storage) in Flutter
Flutter State Management with Riverpod
Manage app state in Flutter using Riverpod 2.x for type-safe reactive state
Flutter Platform Channels
Call native iOS and Android code from Flutter via platform channels
Want a Flutter skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.