KVS TestSmith ๐Ÿ› ๏ธ

Automatic Flutter test generation powered by static analysis.

Pub Package License: MIT Dart Flutter

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:analyzer to accurately detect StatelessWidget, StatefulWidget, Services, Repositories, Controllers, BLoCs, and Cubits.
  • ๐Ÿ“ Folder Mirroring: Tests are generated in a structure that perfectly mirrors your lib/ directory inside your test/ directory.
  • ๐Ÿงช Smart Widget Tests: Automatically generates testWidgets() with pumpWidget() and a MaterialApp wrapper.
  • ๐Ÿ”ฌ 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 mocktail mock 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 --coverage and get a parsed terminal summary of lines covered.

๐Ÿ“ฆ Installation

To use the CLI globally from anywhere on your machine:

dart pub global activate kvs_testsmith

Or, add it as a dev dependency to your specific Flutter project:

dev_dependencies:
  kvs_testsmith: ^1.0.0

๐Ÿš€ Usage

Run the tool from the root of your Flutter project.

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

  1. Download and install Ollama on your machine.
  2. Open your terminal and securely pull the underlying coding model by running:
    ollama run deepseek-coder
    
  3. 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.


Built with โค๏ธ for the Flutter community by the KVS TestSmith Maintainers.

flutter_test_smith