AGENT SKILL
gen-test-dart
Generate Flutter/Dart tests following the project's testing patterns
- License 许可
- MIT
- 最近更新
- 2026-04-15
- Stars
- ★ 2
- Forks
- 0
01 前言 Front Matter
- name
- gen-test-dart
- description
- Generate Flutter/Dart tests following the project's testing patterns
- user-invocable
- true
- disable-model-invocation
- false
03 SKILL.md 正文
Generate Dart Tests
Generate tests for Flutter/Dart source files following this project's conventions.
Arguments
The user specifies source files or features to test.
Steps
- Read the source file(s) to understand the code
- Read 1-2 existing test files of the same type (model, provider, widget, service) to match patterns
- Generate the test file
- Run
flutter test <new_test_file>to verify it passes
Test File Location
Mirror lib/ structure under test/, append _test.dart:
lib/features/subscriptions/models/subscription.dart→test/features/subscriptions/models/subscription_test.dart
Conventions
Structure
import 'package:flutter_test/flutter_test.dart';
void main() {
group('ClassName', () {
test('descriptive behavior name', () {
// Arrange → Act → Assert
});
});
}
Naming
- Descriptive English:
'returns empty list when no matches' - Nested
group()for logical grouping by method/behavior
Model Tests
- Direct instantiation, no dependencies
closeTo(value, tolerance)for floats- Common matchers:
equals(),isTrue,isFalse,isEmpty,contains(),hasLength()
Provider Tests
ProviderContainerwithoverrides,dispose()intearDown- Fake async notifiers extending the real class, overriding
build():
class _FakeSubscriptionsNotifier extends SubscriptionsNotifier {
_FakeSubscriptionsNotifier(this._subscriptions);
final List<Subscription> _subscriptions;
@override
Future<List<Subscription>> build() async => _subscriptions;
}
- Override:
provider.overrideWith(() => _FakeNotifier(data)) - Await async:
await container.read(provider.future)
Widget Tests
testWidgets('description', (WidgetTester tester) async {
await tester.pumpWidget(
ProviderScope(
overrides: [/* overrides */],
child: const MaterialApp(home: Scaffold(body: YourWidget())),
),
);
expect(find.text('Expected'), findsOneWidget);
});
- State checks:
ProviderScope.containerOf(tester.element(find.byType(Widget)))
Service Tests
MockClientfrompackage:http/testing.dart, injected into service constructor
Rules
- Do NOT use Mockito — manual fakes and
MockClientonly - Each test file is self-contained (no shared utility files)
- AAA pattern: Arrange → Act → Assert
flutter_testmatchers only