From Dart and Flutter
Writes and organizes unit tests for Dart/Flutter functions, methods, and classes using package:test. Helps ensure correctness and prevent regressions when creating or fixing logic.
How this skill is triggered — by the user, by Claude, or both
Slash command
/dart-flutter:dart-add-unit-testThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
- [Structuring Test Files](#structuring-test-files)
Organize test files to mirror the lib directory structure to maintain predictability.
test directory at the root of the package._test.dart to the end of all test file names (e.g., lib/src/utils.dart should be tested in test/src/utils_test.dart).integration_test directory at the root of the package.Utilize package:test as the standard testing library for Dart applications.
package:test/test.dart (or package:flutter_test/flutter_test.dart for Flutter).group() function to provide shared context.test() function.expect() function alongside matchers (e.g., equals(), isTrue, throwsA()).async/await syntax. The test runner automatically waits for the Future to complete.setUp() and tearDown() callbacks.package:mockito alongside package:test to generate mock objects, configure fixed scenarios, and verify interactions.Select the appropriate test runner based on the project type and test location.
dart test command.flutter test command.dart test integration_test or flutter test integration_test.Follow this sequential workflow when implementing new test suites. Copy the checklist to track your progress.
test/ directory, ensuring the _test.dart suffix.package:test/test.dart and the target library.main() function.setUp().test() cases grouped by functionality using group().Demonstrates grouping, setup, synchronous, and asynchronous testing.
import 'package:test/test.dart';
import 'package:my_package/calculator.dart';
void main() {
group('Calculator', () {
late Calculator calc;
setUp(() {
calc = Calculator();
});
test('adds two numbers correctly', () {
expect(calc.add(2, 3), equals(5));
});
test('handles asynchronous operations', () async {
final result = await calc.fetchRemoteValue();
expect(result, isNotNull);
expect(result, greaterThan(0));
});
});
}
Demonstrates configuring a mock object for dependency injection testing.
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';
import 'package:mockito/annotations.dart';
import 'package:my_package/api_client.dart';
import 'package:my_package/data_service.dart';
// Generate the mock using build_runner: dart run build_runner build
@GenerateNiceMocks([MockSpec<ApiClient>()])
import 'data_service_test.mocks.dart';
void main() {
group('DataService', () {
late MockApiClient mockApiClient;
late DataService dataService;
setUp(() {
mockApiClient = MockApiClient();
dataService = DataService(apiClient: mockApiClient);
});
test('returns parsed data on successful API call', () async {
// Configure the mock
when(mockApiClient.get('/data')).thenAnswer((_) async => '{"id": 1}');
// Execute the system under test
final result = await dataService.fetchData();
// Verify outcomes and interactions
expect(result.id, equals(1));
verify(mockApiClient.get('/data')).called(1);
});
});
}
npx claudepluginhub flutter/agent-plugins --plugin dart-flutterProvides Dart unit test, Flutter widget test, and golden file test best practices using package:test, package:flutter_test, package:mocktail, and package:bloc_test.
Writes tests for Flutter code following a priority-based approach: Repository → State → Widget. Provides templates for unit tests with mocktail.
Generates mock objects for Dart unit tests using package:mockito and build_runner. Use when testing classes that depend on external services like APIs or databases.