flutter-test
npx machina-cli add skill geekswamp/agent-skills/flutter-test --openclawGenerate Flutter tests following best practices for BLoC-based state management.
Required Test Dependencies
Ensure the project has these dev_dependencies before generating tests:
dev_dependencies:
flutter_test:
sdk: flutter
bloc_test: ^10.0.0
mocktail: ^1.0.4
Default mocking stack for this skill remains:
bloc_testfor BLoC/Cubit behavior tests.mocktailfor repositories/services and interaction verification.
Requirements
General
- Use Flutter test conventions and idioms.
- Always separate:
- Unit tests (BLoC, Cubit, business logic)
- Widget tests (UI behavior, rendering, interaction)
- Tests must be deterministic and isolated.
- Always use English for test names and comments.
- Prefer readability and clarity over brevity.
Unit Test Requirements (State Management)
-
Use
bloc_testfor allBlocorCubittesting. -
Tests must:
- Clearly describe initial state, action, and expected states
- Use
blocTest<TBloc, TState>
-
Each public event or method must have:
- At least one success case
- At least one failure or edge case (if applicable)
-
Avoid testing UI-related logic in unit tests.
-
Use
mocktailfor mocking dependencies:- Repositories
- Data sources
- External services
-
Do NOT use real implementations in unit tests.
Widget Test Requirements
- Use
flutter_testwithWidgetTester. - Widget tests must:
- Verify rendered UI states (loading, success, error)
- Test user interactions (tap, input, scroll)
- Assert visible behavior, not implementation details
- Inject mocked BLoCs using:
BlocProvider.value- or
MultiBlocProvider
- Avoid golden tests unless explicitly requested.
Assertions & Style
- Prefer:
expectfor standard assertionsverify/verifyNeverfrommocktailfor interaction checks
- Avoid excessive
pumpAndSettle; prefer explicitpumpdurations. - Use descriptive test names:
"emits [Loading, Success] when fetch is successful"
Coverage Guidelines
- Aim for minimum ≥80% coverage at the package or feature level.
- Coverage must include:
- All major BLoC state transitions
- At least one widget test per critical UI state
- Do not introduce meaningless assertions just to increase coverage.
- If ≥80% coverage cannot be achieved without testing private details:
- Explain briefly why
- Suggest a refactor (e.g. extract logic into BLoC or service)
Output Expectations
- Generate complete and runnable test files.
- Follow Flutter naming conventions:
*_bloc_test.dart*_widget_test.dart
- Include all required imports.
- Separate Arrange / Act / Assert clearly.
- Output test code only unless explanation is explicitly requested.
When to Load References
- Detailed technical reference: REFERENCE.md.
Unit Tests (BLoC / Cubit)
- BLoC testing patterns: unit_bloc_testing.md
- Mocking with mocktail: mocktail.md
Widget Tests
- Widget testing fundamentals: widget_testing.md
- Testing BLoC-powered widgets: bloc_widget_testing.md
Additional Guidelines
- Prefer testing behavior over structure.
- Avoid testing Flutter framework internals.
- Do not mock Flutter SDK classes unless unavoidable.
- If a widget is hard to test:
- Explain why briefly
- Suggest architectural improvement (e.g. push logic into BLoC).
For auth output contract and concrete auth examples, see REFERENCE.md.
Source
git clone https://github.com/geekswamp/agent-skills/blob/main/skills/flutter-test/SKILL.mdView on GitHub Overview
Generates high-quality unit and widget tests for BLoC-based Flutter apps using bloc_test and mocktail. It emphasizes deterministic, isolated tests and clear separation between BLoC logic and UI tests, helping maintainable test suites.
How This Skill Works
The tool promotes blocTest<TBloc, TState> for bloc/cubit behavior tests and mocktail for mocking dependencies. It guides test organization into *_bloc_test.dart (unit tests) and *_widget_test.dart (widget tests), and recommends injecting mocks via BlocProvider.value or MultiBlocProvider.
When to Use It
- When testing Bloc or Cubit state transitions with bloc_test
- When mocking repositories or services with mocktail
- When validating UI behavior via widget tests (loading, success, error)
- When ensuring deterministic, isolated tests that don't rely on real implementations
- When aiming for ≥80% coverage at feature level
Quick Start
- Step 1: Add dev_dependencies flutter_test, bloc_test, and mocktail to pubspec.yaml
- Step 2: Create *_bloc_test.dart for unit tests and *_widget_test.dart for widget tests
- Step 3: Run flutter test and iterate based on failures
Best Practices
- Write unit tests with clear initial state, action, and expected states using blocTest
- Mock dependencies with mocktail; avoid real implementations in unit tests
- Separate unit tests from widget tests; keep concerns isolated
- Use BlocProvider.value or MultiBlocProvider to inject mocks in widget tests
- Prefer descriptive test names and minimal pump/Settle usage; verify interactions with verify/verifyNever
Example Use Cases
- blocTest<MyBloc, MyState>('emits [Loading, Success] when fetch succeeds', (tester) async { ... })
- Widget test that confirms a Loading indicator shows, then renders content after bloc emits Success
- Mock a Repository to throw an exception and verify Error state and UI feedback
- Test user interactions: tapping a button dispatches an event and updates UI
- Test that mocked services are called with correct parameters using verify