Skip to content

Update testWidgets to use setup and teardown #121917

@pdblasi-google

Description

@pdblasi-google

Currently, testWidgets does a lot of its setup and teardown outside of the package:test's setup and teardown system. This makes it difficult to use the standard setup/teardown functionality for organizing and optimizing widget tests.

Use case

As mentioned in #114468, there are a lot of test properties that would make sense to setup and teardown consistently, but we are unable to because the setup, teardown, and enforcement of state are all done outside of the setup/teardown system.

Proposal

Additions

  • Add setUpWidgets(WidgetTesterCallback callback) to allow for setting up widgets consistently
    • State from these methods (such as pumping a widget) should carry over into the test, allowing the test to focus on actions and expectations on a consistently pumped widget
  • Add tearDownWidgets(WidgetTesterCallback callback) to allow for adding teardowns that need access to the WidgetTester.
  • Add addTearDownWidgets(WidgetTesterCallback callback) to allow for teardowns to be added within a test that need to access the WidgetTester.

Modifications

  • Update TestWidgetsFlutterBinding._runTest and TestWidgetsFlutterBinding._runTestBody to use setups, teardowns, or the widget variants added above to do all of the setup for the test.
  • Update the current invariant checks to be invariant enforcement or remove them completely. If we can reset them, do. If we can't reset them, warn, but don't fail the test.
    • This should also include resetting the various on<PropertyChanged> callbacks for TestPlatformDispatcher and TestFlutterView to prevent leaks between tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our testsframeworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions