kvs_testsmith 1.0.1
kvs_testsmith: ^1.0.1 copied to clipboard
Automatic Flutter test generation powered by static analysis. Generate unit tests, widget tests, and integration tests by analyzing your Dart code using the Dart Analyzer AST.
KVS TestSmith ๐ ๏ธ #
Automatic Flutter test generation powered by static analysis.
A powerful, production-ready CLI tool that automatically generates Flutter unit tests, widget tests, and integration tests by deeply analyzing your Dart source code using the Dart Analyzer AST, and fully implementing working tests with local AI powered by DeepSeek Coder via Ollama.
Think of it as Rails generators, but for Flutter tests.
โจ Features #
- ๐ Project Scanner: Recursively scans
lib/while intelligently ignoring generated files (.g.dart,.freezed.dart,.config.dart) and build directories. - ๐ณ AST Parsing: Uses
package:analyzerto accurately detectStatelessWidget,StatefulWidget, Services, Repositories, Controllers, BLoCs, and Cubits. - ๐ Folder Mirroring: Tests are generated in a structure that perfectly mirrors your
lib/directory inside yourtest/directory. - ๐งช Smart Widget Tests: Automatically generates
testWidgets()withpumpWidget()and aMaterialAppwrapper. - ๐ฌ Smart Unit Tests: Generates
group()+test()stubs implementing the AAA pattern (Arrange, Act, Assert) for every public method. - ๐ค AI Test Implementation (Local): Automatically implement full working test files (bypassing stubs) using local Ollama (deepseek-coder) with intelligent UI Element extraction from your AST.
- ๐ญ Mock Generation: Automatically generates
mocktailmock classes inline for your services. - ๐ Watch Mode: Watches
lib/and auto-generates tests live as you create or modify files. - ๐ Coverage Support: Run
flutter test --coverageand get a parsed terminal summary of lines covered.
๐ฆ Installation #
Global Usage (Recommended for Users) #
To use the CLI globally from anywhere on your machine, activate it via Pub:
dart pub global activate kvs_testsmith
Important
To run the global command, ensure your Dart/Pub cache bin directory is in your system PATH. See the Troubleshooting section below if you get a command not found error.
Local/Development Usage (Recommended for Developers) #
If you are working directly in this repository, you can activate it from source to use the kvs_testsmith command globally:
-
Activate from Source:
dart pub global activate --source path . -
Using the convenience script:
./kvs_testsmith generate -
Directly via Dart:
dart run kvs_testsmith:kvs_testsmith generate
๐ Usage #
Run the tool from the root of your Flutter project. If you have it activated globally, you can just use kvs_testsmith. Otherwise, use ./kvs_testsmith (if in the repo) or dart run kvs_testsmith:kvs_testsmith.
Generate all missing tests #
kvs_testsmith generate
Advanced Generation Options #
# Generate only widget tests
kvs_testsmith generate --widgets
# Generate only unit tests
kvs_testsmith generate --unit
# Generate only for files that don't already have tests
kvs_testsmith generate --only-missing
# Combine flags
kvs_testsmith generate --unit --only-missing
Watch Mode #
Leave this running in a terminal. As you add or modify Dart files in your lib/ directory, TestSmith will automatically generate tests for them.
kvs_testsmith watch
๐ค AI Test Implementation (DeepSeek Coder + Ollama) #
KVS TestSmith can leverage a local AI to fully implement your interaction test files instead of just generating structural AAA stubs. The orchestrator will parse your AST, detect interactive UI elements (like ElevatedButton, TextField, GestureDetector) and pipe them to the local LLM.
Ollama Setup Instructions
- Download and install Ollama on your machine.
- Open your terminal and securely pull the underlying coding model by running:
ollama run deepseek-coder - Once Ollama is active locally, you can utilize the TestSmith AI feature:
kvs_testsmith generate --ai
Coverage Report #
Runs your test suite with coverage enabled and prints a clean summary to the terminal.
kvs_testsmith coverage
๐๏ธ How It Works (Examples) #
KVS TestSmith reads your code structure and generates tailored tests.
1. Widget Code โ Widget Test #
If you have a widget at lib/features/auth/ui/login_page.dart:
// Generated by TestSmith at test/features/auth/ui/login_page_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/features/auth/ui/login_page.dart';
void main() {
group('LoginPage', () {
testWidgets('renders correctly', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: const LoginPage(),
),
);
expect(find.byType(LoginPage), findsOneWidget);
});
});
}
2. Service/Repository Code โ Unit Test #
If you have a repository at lib/features/auth/data/auth_repository.dart with a login() method:
// Generated by TestSmith at test/features/auth/data/auth_repository_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:my_app/features/auth/data/auth_repository.dart';
class MockAuthRepository extends Mock implements AuthRepository {}
void main() {
group('AuthRepository', () {
late AuthRepository sut; // System Under Test
setUp(() {
sut = AuthRepository();
});
test('should be instantiated', () {
expect(sut, isNotNull);
});
// TestSmith detected your 'login' method!
test('login works correctly', () {
// TODO: Arrange
// TODO: Act
// final result = sut.login();
// TODO: Assert
// expect(result, expectedValue);
});
});
}
๐ค Contributing #
We welcome contributions! Please see CONTRIBUTING.md for details on how to set up the project and submit pull requests.
If you find a bug or have a feature request, please open an issue.
๐ License #
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Troubleshooting #
zsh: command not found: kvs_testsmith #
If you get this error after running dart pub global activate, your system doesn't know where the Dart binaries are located.
Fix for macOS/Linux:
Add the following line to your ~/.zshrc or ~/.bash_profile:
export PATH="$PATH":"$HOME/.pub-cache/bin"
Then restart your terminal or run source ~/.zshrc.
Fix for Windows:
Add %USERPROFILE%\AppData\Local\Pub\Cache\bin to your system Environment Variables (PATH).
Built with โค๏ธ for the Flutter community by the KVS TestSmith Maintainers.