Testing
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼
tags : #flutter #coding #testing
references : Flutter
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼
📘 Flutter Testing
# Flutter Testing
📌 Overview
- Purpose Ensure Flutter apps run correctly, both in UI and logic.
- Types of Tests in Flutter
- Unit tests → Test small pieces of logic (functions, classes, blocs,
providers).
- Widget tests → Test individual widgets and UI behavior.
- Integration tests → Test full app flows (end-to-end, navigation, API calls).
---
🧩 Unit Testing
- Scope: Dart functions, business logic, state management.
- Tools: `flutter_test` package (built-in).
- Example:
```dart
test('Counter increments', () {
var counter = Counter();
counter.increment();
expect(counter.value, 1);
});
✅ Fast, runs in isolation
❌ Doesn’t test UI
🎨 Widget Testing
Scope: UI components (buttons, text fields, forms, navigation).
Tools: flutter_test
Example:
testWidgets('Finds text and taps button', (tester) async {
await tester.pumpWidget(MyApp());
expect(find.text('Hello'), findsOneWidget);
await tester.tap(find.byType(ElevatedButton));
await tester.pump();
expect(find.text('Clicked!'), findsOneWidget);
});
✅ Tests UI without running on real device
❌ Can be tricky with async / animations
🔗 Integration Testing
Scope: Full app flow (login → dashboard → API → logout).
Tools: integration_test package
Example:
testWidgets('Full login flow', (tester) async {
await tester.pumpWidget(MyApp());
await tester.enterText(find.byKey(Key('email')), '
[email protected]');
await tester.enterText(find.byKey(Key('password')), '123456');
await tester.tap(find.text('Login'));
await tester.pumpAndSettle();
expect(find.text('Welcome'), findsOneWidget);
});
✅ Covers real-world user journeys
❌ Slower, more fragile
🎯 Responsibilities
Frontend Dev: Unit tests + widget tests (must), integration tests (shared).
QA/Team: Manual tests, API/load/security/performance.
🛠️Tools & Packages
flutter_test → Unit & widget testing (comes with Flutter).
integration_test → End-to-end testing.
mockito or mocktail → Mock dependencies.
bloc_test → For BLoC state management testing.
golden_toolkit → Visual/golden tests (UI snapshots).
🚀 Best Practices
1. Write tests alongside features, not after.
2. Cover happy path + edge cases.
3. Keep tests fast and independent.
4. Use mocks/fakes for APIs.
5. Aim for high coverage but don’t chase 100%.
✅ Checklist
Unit tests for all business logic
Widget tests for critical UI components
Integration tests for main flows
Mocks used where needed
Tests run in CI/CD pipeline
---
This way you’ll have **theory + examples + best practices** in one Obsidian note.
👉 Do you want me to also make you a **shorter Obsidian “daily-use” testing
template** (more like a checklist you copy when writing new tests)?