Repository navigation
Move away from TestSemantics and towards fine grained semantics testing #184367
Description
Background
TestSemantics is used to quickly check that dumping a semantics tree produces some exact string. This is easy to set up, but is often a cause of unhelpful failures. For example, you may have made a small change to the widgets returned from a build method only to be greeted by a large wall of text about how the semantics no longer matches.
How to test semantics instead
Instead, find the exact semantics node that is relevant to the given test using tester.semantics.find. Then use isSemantics to verify only the semantics properties that are relevant to your test. Proceed down the semantics tree as far as is relevant using the children parameter of isSemantics with a list of further calls to isSemantics.
Here is an example, or see the example PRs linked below.
expect(
tester.semantics.find(find.bySemanticsLabel('Alert'))
isSemantics(
label: 'Alert',
role: SemanticsRole.dialog,
namesRoute: true,
scopesRoute: true,
children: <Matcher>[
isSemantics(
hasImplicitScrolling: true,
children: <Matcher>[
isSemantics(label: 'The title'),
isSemantics(label: 'The message'),
],
),
],
),
);Migrating
This came up in #183790 (comment), where it was decided that we would duplicate TestSemantics in the new material_ui package instead of exposing it publicly in flutter_test, in order to avoid encouraging other people to use it. We should migrate all usages across flutter/flutter and material_ui to use the recommendation above, and then remove both instances of TestSemantics.
Resources
- Example: [cupertino_ui] Re-enable action_sheet_test.dart packages#12055
- Example: [cupertino_ui] Re-enable dialog_test.dart packages#12057
List of locations to migrate
rg -l TestSemantics
Test files that use TestSemantics
- test/widgets/visibility_test.dart
- PR:
- test/widgets/text_test.dart
- PR:
- test/widgets/draggable_test.dart
- PR:
- test/widgets/semantics_merge_test.dart
- PR:
- test/widgets/gesture_detector_semantics_test.dart
- PR:
- test/widgets/semantics_1_test.dart
- PR:
- test/widgets/sliversemantics_test.dart
- PR:
- test/widgets/semantics_tester_generate_test_semantics_expression_for_current_semantics_tree_test.dart
- PR:
- test/widgets/semantics_4_test.dart
- PR:
- test/widgets/custom_painter_test.dart
- PR:
- test/widgets/modal_barrier_test.dart
- PR:
- test/widgets/table_test.dart
- PR:
- test/material/bottom_sheet_test.dart
- PR:
- test/material/outlined_button_test.dart
- PR:
- test/widgets/routes_test.dart
- PR:
- test/widgets/single_child_scroll_view_test.dart
- PR:
- test/material/elevated_button_test.dart
- PR:
- test/widgets/semantics_7_test.dart
- PR:
- test/widgets/semantics_tester_test.dart
- PR:
- test/rendering/proxy_box_test.dart
- PR:
- test/widgets/opacity_test.dart
- PR:
- test/widgets/scrollable_semantics_traversal_order_test.dart
- PR:
- test/widgets/semantics_2_test.dart
- PR:
- test/widgets/editable_text_test.dart
- PR:
- test/widgets/raw_tooltip_test.dart
- PR:
- test/widgets/sliver_semantics_test.dart
- PR:
- test/widgets/focus_scope_test.dart
- PR:
- test/widgets/semantics_5_test.dart
- PR:
- test/material/slider_test.dart
- PR:
- test/widgets/selectable_region_test.dart
- PR:
- test/widgets/simple_semantics_test.dart
- PR:
- test/widgets/semantics_zero_surface_size_test.dart
- PR:
- test/material/dialog_test.dart
- PR:
- test/widgets/semantics_test.dart
- PR:
- test/widgets/semantics_11_test.dart
- PR:
- test/widgets/semantics_8_test.dart
- PR:
- test/material/switch_list_tile_test.dart
- PR:
- test/material/list_tile_test.dart
- PR:
- test/material/radio_list_tile_test.dart
- PR:
- test/widgets/scrollable_semantics_test.dart
- PR:
- test/widgets/semantics_child_configs_delegate_test.dart
- PR:
- test/material/text_field_test.dart
- PR:
- test/material/user_accounts_drawer_header_test.dart
- PR:
- test/widgets/shortcuts_test.dart
- PR:
- test/widgets/semantics_clipping_test.dart
- PR:
- test/material/segmented_button_test.dart
- PR:
- test/widgets/semantics_traversal_test.dart
- PR:
- test/widgets/overlay_portal_test.dart
- PR:
- test/material/chip_test.dart
- PR:
- test/widgets/semantics_3_test.dart
- PR:
- test/material/radio_test.dart
- PR:
- test/widgets/semantics_tester.dart
- PR:
- test/widgets/focus_traversal_test.dart
- PR:
- test/widgets/semantics_6_test.dart
- PR:
- test/widgets/image_test.dart
- PR:
- test/widgets/semantics_event_test.dart
- PR:
- test/cupertino/slider_test.dart
- PR:
- test/material/data_table_test.dart
- PR:
- test/widgets/implicit_semantics_test.dart
- PR:
- test/cupertino/dialog_test.dart
- PR:
- test/material/icon_button_test.dart
- PR:
- test/material/material_button_test.dart
- PR:
- test/material/popup_menu_test.dart
- PR:
- test/material/text_button_test.dart
- PR:
- test/cupertino/button_test.dart
- PR:
- test/material/filled_button_test.dart
- PR:
- test/cupertino/action_sheet_test.dart
- PR:
- test/material/tooltip_theme_test.dart
- PR:
- test/material/menu_anchor_test.dart
- PR:
- test/semantics/semantics_owner_test.dart
- PR:
- test/cupertino/text_field_test.dart
- PR:
- test/cupertino/segmented_control_test.dart
- PR:
- test/cupertino/sliding_segmented_control_test.dart
- PR:
- test/material/floating_action_button_test.dart
- PR:
- test/semantics/traversal_order_test.dart
- PR:
- test/material/bottom_navigation_bar_test.dart
- PR:
- test/material/card_test.dart
- PR:
- test/material/app_bar_test.dart
- PR:
- test/material/raw_material_button_test.dart
- PR:
- test/material/navigation_rail_test.dart
- PR:
- test/material/flexible_space_bar_test.dart
- PR:
- test/material/selectable_text_test.dart
- PR:
- test/material/search_test.dart
- PR:
- test/material/toggle_buttons_test.dart
- PR:
- test/material/sliver_app_bar_test.dart
- PR:
- test/material/app_bar_sliver_test.dart
- PR:
- test/material/dropdown_menu_test.dart
- PR:
- test/material/tabs_test.dart
- PR:
- test/material/dropdown_test.dart
- PR:
Activity
[-]Move away TestSemantics and towards fine grained semantics testing[/-][+]Move away from TestSemantics and towards fine grained semantics testing[/+]VaradGupta23 commentedon Mar 31, 2026
saurabh-mirajkar commentedon Apr 1, 2026
Thanks for the detailed report and proposal. Labelling for
team-frameworkto investigate further.Warn about the use of TestSemantics (#184369)
Warn about the use of TestSemantics (flutter#184369)
[cupertino_ui] Re-enable action_sheet_test.dart (#12055)
[cupertino_ui] Re-enable dialog_test.dart (#12057)
Warn about the use of TestSemantics (#184369)