widgetbook_golden_test_alchemist
Automatically generate Flutter golden tests from Widgetbook use cases using Alchemist.
Overview
This library provides an Alchemist adapter for automating the creation of golden tests for Flutter widgets using Widgetbook use cases. It leverages the generated directory files from widgetbook_generator to discover use cases and render them into golden images using Alchemist's powerful rendering engine.
It is built upon widgetbook_golden_test_core, which provides the underlying engine for discovery and mocking dependencies.
Usage
- Generate your Widgetbook directories using
widgetbook_generator:
dart run build_runner build -d
- Import the directories file generated.
- Call
runAlchemistWidgetbookGoldenTestsinside themainfunction of a test file.
import 'package:widgetbook_golden_test_alchemist/widgetbook_golden_test_alchemist.dart';
import '../widgetbook/main.directories.g.dart';
void main() {
runAlchemistWidgetbookGoldenTests(
nodes: directories,
properties: WidgetbookGoldenTestsProperties(),
);
}
- Generate the golden files by running:
flutter test <path-to-test-file> --update-goldens
Features
- Alchemist Integration: Leverages Alchemist for rendering, providing a robust and reliable golden testing experience.
- Automatic Discovery: All Widgetbook use cases are automatically discovered and tested.
- Network Image Mocking: Handles network images for reliable tests. You can simulate loading and error states using
WidgetbookGoldenTestsProperties. - Knob Support: Supports Widgetbook knobs by mocking the internal state.
- Customization: Customize properties at the group level via
WidgetbookGoldenTestsPropertiesor per use-case usingWidgetbookGoldenTestBuilder. - Play Actions: Use
GoldenPlayActionwithinWidgetbookGoldenTestBuilderto capture snapshots after interactions (e.g., scrolling, tapping). - Widgetbook Addons: Apply and merge Widgetbook addons to test different themes, locales, and text scales.
How It Works
- Discovery: Use cases are defined and auto-generated in the directories file by
widgetbook_generator. - Traversal: The
runAlchemistWidgetbookGoldenTestsfunction traverses the provided Widgetbook nodes. - Mocking: It sets up a mocked environment using
goldenTestZoneRunner, which includes a mockedHttpClientfor network images. - Execution: Each use case is pumped into a
MockedWidgetbookCasewhich provides the necessaryWidgetbookScopeand theme context. - Verification: The
WidgetbookGoldenAlchemistRendereruses Alchemist'sgoldenTestto perform the visual comparison.
Limitations
- Argument
goldenFinderinGoldenPlayActionis not supported yet. - Running non CI tests may affect CI golden tests. This is still under investigation.