unit_test_generator 1.0.1
unit_test_generator: ^1.0.1 copied to clipboard
Auto-generate AAA-structured unit test boilerplate for Flutter repositories, data sources, and use cases.
🧪 Unit Test Generator #
Unit Test Generator automatically creates unit test templates (with the Arrange–Act–Assert pattern) for your
Dart and Flutter projects — especially for repositories, remote data sources, and use cases.
Features #
✅ Detects classes ending with:
RepositoryRemoteDataSourceUseCase
✅ Generates .test.dart files with:
- AAA structure (Arrange – Act – Assert)
- Grouped test blocks per class
- Fake variable setup for common parameter types
- Ready-to-use
flutter_testandmockitoimports
✅ Works with both Dart and Flutter projects.
Installation #
Add the package to your dev_dependencies:
dev_dependencies:
my_test_generator: ^1.0.0
build_runner: ^2.4.9
Usage #
Run the code generator:
flutter pub run build_runner build
or keep it running while you code:
flutter pub run build_runner watch
Example #
class UserRepository {
Future<String> getUser(int id) async => 'User $id';
Future<void> saveUser(String name) async {}
}
The Generated File Will Be like this
// lib/data/repositories/user_repository.test.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:my_app/data/repositories/user_repository.dart';
void main() {
group('UserRepositoryTest', () {
late UserRepository instance;
setUp(() {
instance = UserRepository();
});
test('should call getUser', () async {
// Arrange
const int id = 1;
// Act
final result = await instance.getUser(id);
// Assert
expect(result, isNotNull);
});
test('should call saveUser', () async {
// Arrange
const String name = 'test_name';
// Act
await instance.saveUser(name);
// Assert
// Verify interactions or side effects
});
});
}
How It Works #
unit_test_generator uses the Dart build system and source_gen to:
- Parse your code using the analyzer API.
- Detect public methods inside target classes.
- Automatically generate matching unit test templates with AAA comments.
- Save them as .test.dart files beside the original source file.
AAA Pattern #
Each test follows the Arrange → Act → Assert pattern:
test('should fetch data', () async {
// Arrange
const int id = 1;
// Act
final result = await instance.fetchData(id);
// Assert
expect(result, isNotNull);
});