test_builder 0.1.2 copy "test_builder: ^0.1.2" to clipboard
test_builder: ^0.1.2 copied to clipboard

Dart package that provides a Builder pattern for Flutter tests

Test Builder #

Test Builder is a Dart package that provides a Builder pattern for Flutter tests. It simplifies the process of creating and managing test cases for Flutter widgets and screens.

Features #

  • Flexible test setup using Builder and Director patterns
  • Support for Material widgets and screens
  • Easy management of test arguments and widget construction
  • Reusable components for common testing scenarios

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  test_builder: ^0.1.2
copied to clipboard

Then run:

flutter pub get
copied to clipboard

Usage #

Basic Usage #

  1. Import the package:
import 'package:test_builder/test_builder.dart';
copied to clipboard
  1. Create a Director for your widget or screen:
late MaterialScreenWidgetDirector<(String, String, String, VoidCallback)> director;

setUp(() => director = MaterialScreenWidgetDirector<(String, String, String, VoidCallback)>()
  ..setUnitFactory((args) => CustomScreen(
        title: args.$1,
        message: args.$2,
        buttonText: args.$3,
        onButtonPressed: args.$4,
      )));
copied to clipboard
  1. Set up your test cases:
group('Display confirmation', () {
  setUp(() => director.setArgsFactory(() => ('Test Title', 'Test Message', 'Test Button', () {})));

  testWidgets('CustomScreen is displayed correctly', (WidgetTester tester) async {
    await tester.pumpWidget(director.construct());
    await tester.pumpAndSettle();

    expect(find.text('Test Title'), findsOneWidget);
    expect(find.text('Test Message'), findsOneWidget);
    expect(find.text('Test Button'), findsOneWidget);
  });
});
copied to clipboard

Advanced Usage #

For more complex scenarios, you can create custom Directors:

class OtherCustomScreenDirector extends MaterialScreenWidgetDirector<(String, String, List<String>, VoidCallback)> {
  bool pressed = false;

  OtherCustomScreenDirector() {
    setUnitFactory((args) => OtherCustomScreen(
          title: args.$1,
          subtitle: args.$2,
          items: args.$3,
          onButtonPressed: args.$4,
        ));
  }

  void useDefaultScreen() => setArgs(('Test Title', 'Test Subtitle', ['Item 1', 'Item 2', 'Item 3'], () {}));

  void useScreenWithPressableButton() =>
      setArgs(('Test Title', 'Test Subtitle', ['Item 1', 'Item 2', 'Item 3'], () => pressed = true));
}
copied to clipboard

Examples #

Check the test directory for more usage examples:

  • custom_button_test.dart: Testing a simple custom button
  • custom_screen_test.dart: Testing a custom screen with multiple components
  • other_custom_screen_test.dart: Testing a more complex custom screen using a custom Director

Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

0
likes
150
points
20
downloads

Publisher

verified publishercaph.jp

Weekly Downloads

2024.07.06 - 2025.01.18

Dart package that provides a Builder pattern for Flutter tests

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on test_builder