Skip to content

Allow debugDefaultTargetPlatformOverride to be resetted with addTearDown #110488

Open
@passsy

Description

@passsy
void main() {
  testWidgets('reset debugDefaultTargetPlatformOverride with tearDown', (tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
    addTearDown(() {
      debugDefaultTargetPlatformOverride = null;
    });
  });
}

produces

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
The value of a foundation debug variable was changed by the test.

When the exception was thrown, this was the stack:
#0      debugAssertAllFoundationVarsUnset.<anonymous closure> (package:flutter/src/foundation/debug.dart:30:7)
#1      debugAssertAllFoundationVarsUnset (package:flutter/src/foundation/debug.dart:32:4)
#2      TestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:861:12)
#3      AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1274:11)
#4      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:848:7)
<asynchronous suspension>

The test description was:
  reset debugDefaultTargetPlatformOverride with tearDown
════════════════════════════════════════════════════════════════════════════════════════════════════

I was expecting addTearDown to be the correct solution because setting the override to null might not be executed at the end of the test when the test fails.

In fact - without addTearDown - all remaining tests will fail with the exception above when this test fails, because it never executes debugDefaultTargetPlatformOverride = null;.

void main() {
  testWidgets('reset debugDefaultTargetPlatformOverride with tearDown', (tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
    // ... test implementation
	expect(true, isFalse);
	
    // might not be called
    debugDefaultTargetPlatformOverride = null;
  });

  testWidgets('default target platform is android', (tester) async {
    // fails with exception although this test is 100% correct
    expect(true, isTrue);
  });
}

How to fix

_verifyInvariants should be called after executing all addTearDown.


[✓] Flutter (Channel master, 3.1.0-0.0.pre.2434, on macOS 12.5.1 21G83
    darwin-arm, locale en-DE)
    • Flutter version 3.1.0-0.0.pre.2434 on channel master at
      /opt/homebrew/Caskroom/flutter/3.0.4/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c8569b6e98 (6 days ago), 2022-08-23 13:50:09 -0700
    • Engine revision c9d0a01281
    • Dart version 2.19.0 (build 2.19.0-127.0.dev)
    • DevTools version 2.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: tests"flutter test", flutter_test, or one of our testsfound in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-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