testWidgets function

  1. @isTest
void testWidgets (String description, WidgetTesterCallback callback, { bool skip: false, Timeout timeout })

Runs the callback inside the Flutter test environment.

Use this function for testing custom StatelessWidgets and StatefulWidgets.

The callback can be asynchronous (using async/await or using explicit Futures).

This function uses the test function in the test package to register the given callback as a test. The callback, when run, will be given a new instance of WidgetTester. The find object provides convenient widget Finders for use with the WidgetTester.

Sample code

    testWidgets('MyWidget', (WidgetTester tester) async {
      await tester.pumpWidget(new MyWidget());
      await tester.tap(find.text('Save'));
      expect(find.text('Success'), findsOneWidget);
    });

Implementation

@isTest
void testWidgets(String description, WidgetTesterCallback callback, {
  bool skip = false,
  test_package.Timeout timeout
}) {
  final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
  final WidgetTester tester = WidgetTester._(binding);
  timeout ??= binding.defaultTestTimeout;
  test_package.test(
    description,
    () {
      tester._recordNumberOfSemanticsHandles();
      test_package.addTearDown(binding.postTest);
      return binding.runTest(
        () => callback(tester),
        tester._endOfTestVerifications,
        description: description ?? '',
      );
    },
    skip: skip,
    timeout: timeout
  );
}