skills/dart/dart-add-unit-test/SKILL.md
Write unit tests with mocking and coverage analysis for pure Dart projects and CLI apps. Use when testing business logic, generating mocks with Mockito or mocktail, or measuring test coverage.
npx skillsauth add dhruvanbhalara/skills dart-add-unit-testInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Utilize package:test as the standard testing library for Dart CLI and backend applications.
lib directory structure. Append _test.dart to test files.group() function.setUp() and tearDown() for shared test state.group('$ClassName', ...)| Criteria | mocktail (Recommended) | mockito |
|---|---|---|
| Code generation | Not needed | Requires build_runner |
| Setup speed | Fast (extend Mock) | Slower (annotation + codegen) |
| Null safety | Full support | Full support |
| API style | when(() => mock.method()) | when(mock.method()) |
| Best for | Most projects, fast iteration | Large projects with complex APIs |
Rule: Default to mocktail (no codegen, faster setup). Use mockito only when you need @GenerateNiceMocks for complex APIs with many overloads.
import 'package:mocktail/mocktail.dart';
// Create mock — no code generation needed
class MockUserRepository extends Mock implements UserRepository {}
void main() {
late MockUserRepository mockRepo;
setUp(() {
mockRepo = MockUserRepository();
});
test('fetches user successfully', () {
// Arrange
when(() => mockRepo.getUser('1'))
.thenAnswer((_) async => const User(id: '1', name: 'Alice'));
// Act
final user = await mockRepo.getUser('1');
// Assert
expect(user.name, 'Alice');
verify(() => mockRepo.getUser('1')).called(1);
});
}
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
@GenerateNiceMocks([MockSpec<UserRepository>()])
import 'user_test.mocks.dart'; // Generated by: dart run build_runner build
| API | Purpose |
|---|---|
| verify(() => mock.method()).called(1) | Assert method was called exactly once |
| verifyNever(() => mock.method()) | Assert method was never called |
| verifyNoMoreInteractions(mock) | Assert no unchecked interactions remain |
| verifyInOrder([...]) | Assert methods were called in specific order |
| reset(mock) | Clear all stubs and interactions |
For methods with custom types as parameters:
setUpAll(() {
registerFallbackValue(const User(id: '0', name: 'fallback'));
});
dart test --coverage=coverage or flutter test --coverage.dart-coverage skill for detailed LCOV/HTML report generation.Follow this sequential workflow when testing a Dart feature. Copy the checklist to track progress.
_test.dart.dart run build_runner build to generate mocks if using Mockito.dart test.dart test --coverage=coverage and verify coverage targets are met.development
Perform REST API networking operations (GET, POST, PUT, DELETE) using the lightweight and robust standard `http` package, including platform configurations and background parsing models.
development
Configure internationalization and localization support using Flutter's built-in l10n system, App Resource Bundle (ARB) files, and ICU formatting syntax.
development
Create model classes with fromJson/toJson using dart:convert and Dart 3 pattern matching. Use when manually mapping JSON to classes, parsing HTTP responses, or choosing between manual and code-generated serialization.
data-ai
Diagnose and fix Flutter layout constraint violations (RenderFlex overflow, unbounded height/width, ParentData misuse). Use when encountering layout exceptions, yellow-black overflow stripes, or red error screens.