testWidgets function Null safety

  1. @isTest
void testWidgets(
  1. String description,
  2. WidgetTesterCallback callback,
  3. {bool? skip,
  4. Timeout? timeout,
  5. Duration? initialTimeout,
  6. bool semanticsEnabled = true,
  7. TestVariant<Object?> variant = const DefaultTestVariant(),
  8. dynamic tags}

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).

There are two kinds of timeouts that can be specified. The timeout argument specifies the backstop timeout implemented by the test package. If set, it should be relatively large (minutes). It defaults to ten minutes for tests run by flutter test, and is unlimited for tests run by flutter run; specifically, it defaults to TestWidgetsFlutterBinding.defaultTestTimeout.

The initialTimeout argument specifies the timeout implemented by the flutter_test package itself. If set, it may be relatively small (seconds), as it is automatically increased for some expensive operations, and can also be manually increased by calling AutomatedTestWidgetsFlutterBinding.addTime. The effective maximum value of this timeout (even after calling addTime) is the one specified by the timeout argument.

In general, timeouts are race conditions and cause flakes, so best practice is to avoid the use of timeouts in tests.

If the semanticsEnabled parameter is set to true, WidgetTester.ensureSemantics will have been called before the tester is passed to the callback, and that handle will automatically be disposed after the callback is finished. It defaults to true.

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.

When the variant argument is set, testWidgets will run the test once for each value of the TestVariant.values. If variant is not set, the test will be run once using the base test environment.

If the tags are passed, they declare user-defined tags that are implemented by the test package.

See also:

Sample code

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


void testWidgets(
  String description,
  WidgetTesterCallback callback, {
  bool? skip,
  test_package.Timeout? timeout,
  Duration? initialTimeout,
  bool semanticsEnabled = true,
  TestVariant<Object?> variant = const DefaultTestVariant(),
  dynamic tags,
}) {
  assert(variant != null);
  assert(variant.values.isNotEmpty, 'There must be at least one value to test in the testing variant.');
  final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
  final WidgetTester tester = WidgetTester._(binding);
  for (final dynamic value in variant.values) {
    final String variationDescription = variant.describeValue(value);
    // IDEs may make assumptions about the format of this suffix in order to
    // support running tests directly from the editor (where they may have
    // access to only the test name, provided by the analysis server).
    // See https://github.com/flutter/flutter/issues/86659.
    final String combinedDescription = variationDescription.isNotEmpty
        ? '$description (variant: $variationDescription)'
        : description;
      () {
        tester._testDescription = combinedDescription;
        SemanticsHandle? semanticsHandle;
        if (semanticsEnabled == true) {
          semanticsHandle = tester.ensureSemantics();
        return binding.runTest(
          () async {
            binding.reset(); // TODO(ianh): the binding should just do this itself in _runTest
            Object? memento;
            try {
              memento = await variant.setUp(value);
              await callback(tester);
            } finally {
              await variant.tearDown(value, memento);
          description: combinedDescription,
          timeout: initialTimeout,
      skip: skip,
      timeout: timeout ?? binding.defaultTestTimeout,
      tags: tags,