dart-testing
Dart Testing Automation
Goal
Analyzes Dart and Flutter codebases to architect, implement, and execute robust test suites. Generates unit, component, and integration tests utilizing package:test and package:mockito. Configures mock objects, structures test groups, and executes validation loops using dart test or flutter test to ensure code reliability and prevent regressions.
Decision Logic
Evaluate the target repository to determine the appropriate testing strategy:
- Project Type:
- If
pubspec.yamlcontainssdk: flutter: Useflutter_testand execute viaflutter test. - If pure Dart (Server/CLI/Web): Use
package:testand execute viadart test.
- If
- Test Scope:
- Isolated Logic (Functions/Classes): Implement Unit Tests. Isolate the system under test using
package:mockito. - UI/Multi-class interactions: Implement Component/Widget Tests. Use
flutter_testfor widget pumping or standard component instantiation. - Full Application Flow: Implement Integration Tests. Use
flutter_driverorintegration_testpackage.
- Isolated Logic (Functions/Classes): Implement Unit Tests. Isolate the system under test using
Instructions
-
Analyze Context and Configure Dependencies Inspect
pubspec.yaml. Ensure the required testing and mocking packages are present underdev_dependencies.dev_dependencies: test: ^1.24.0 # Use flutter_test for Flutter projects mockito: ^5.4.4 build_runner: ^2.4.8 -
Generate Mock Objects Identify external dependencies (e.g., API clients, database repositories) of the system under test. Create a test file and use the
@GenerateMocksannotation.import 'package:mockito/annotations.dart'; import 'package:my_app/api_client.dart'; ([ApiClient]) void main() {}Execute the build runner to generate the mock files:
dart run build_runner build --delete-conflicting-outputsSTOP AND ASK THE USER: If you do not have execution capabilities in the current environment, provide the
build_runnercommand to the user and wait for them to confirm successful generation before proceeding. -
Implement the Test Suite Write tests using the
testpackage. Group related tests and use descriptive names. Utilizeexpect()with appropriate matchers.import 'package:test/test.dart'; import 'package:mockito/mockito.dart'; import 'package:my_app/data_service.dart'; import 'api_client.mocks.dart'; // Generated mock file void main() { group('DataService -', () { late DataService dataService; late MockApiClient mockApiClient; setUp(() { mockApiClient = MockApiClient(); dataService = DataService(apiClient: mockApiClient); }); test('fetchData returns parsed data on successful API call', () async { // Arrange when(mockApiClient.get('/data')).thenAnswer((_) async => '{"status": "ok"}'); // Act final result = await dataService.fetchData(); // Assert expect(result, equals({'status': 'ok'})); verify(mockApiClient.get('/data')).called(1); }); test('fetchData throws Exception on API failure', () async { // Arrange when(mockApiClient.get('/data')).thenThrow(Exception('Network Error')); // Act & Assert expect( () => dataService.fetchData(), throwsA(isA<Exception>()), ); }); }); } -
Execute Tests and Validate Run the test suite using the appropriate CLI command. Use flags to target specific directories or tags if necessary.
# For pure Dart projects dart test test/data_service_test.dart # For Flutter projects flutter test test/data_service_test.dart -
Validate-and-Fix Loop Parse the output of the test execution.
- If tests pass, proceed to the next task or finalize the implementation.
- If tests fail, analyze the stack trace, identify the mismatch between expected and actual values, modify the source code or the test assertions accordingly, and re-run the tests until they pass.
-
Configure Continuous Integration (Optional) If requested, generate a GitHub Actions workflow to automate test execution.
name: Dart CI on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dart-lang/setup-dart@v1 - run: dart pub get - run: dart test
Constraints
- DO write tests using the
testpackage with descriptivegroupandtestnames. - DO use
expect()with appropriate matchers (e.g.,completion,throwsA,isA). - DO run tests via
dart test(orflutter test) and use flags for specific directories or tags when applicable. - PREFER mock objects or fakes (via
package:mockito) to isolate the system under test. Never make real HTTP requests or database mutations in unit tests. - DO NOT write monolithic test functions; strictly separate Arrange, Act, and Assert phases.
- DO NOT leave unresolved type promotion failures in test files; ensure sound null safety is respected.
- Related Skills:
dart-static-analysis,dart-code-generation.