Repository: flutter
Issue: Error in integration_test on timeout: '!inTest': is not true.
Labels: framework,f: integration_test,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.16,found in release: 3.18

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

In any flutter project follow below steps:

1. Create file `test_driver/main_test.dart`
```
import 'dart:io';
import 'package:integration_test/integration_test_driver_extended.dart';

Future<void> main() async {
  print('Starting test driver');

  try {
    var driver = await integrationDriver(
      onScreenshot: (
        String screenshotName,
        List<int> screenshotBytes, [
        Map<String, Object?>? metadata,
      ]) async {
        final File image = await File('screenshots/$screenshotName.png')
            .create(recursive: true);
        image.writeAsBytesSync(screenshotBytes);
        return true;
      },
    );
  } catch (err) {
    print('Error occurred $err');
  }
}
```
2. Create file `integration_/main.dart` and add below code:
```
import 'dart:async';

import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import '../lib/main.dart' as app_main;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  group("All tests", () {
    setUpAll(() async {
      print("Setup all is run");
      app_main.main();
    });
    testWidgets("first test", (widgetTester) async {
      // This should get successful.
      await widgetTester.pumpAndSettle();
      print("First test is run");
    });
    testWidgets("second test", (widgetTester) async {
      // This should get successful.
      await widgetTester.pumpAndSettle();
      print("Second test is run");
    });
    testWidgets("third test", (widgetTester) async {
      // This will throw test error.
      await widgetTester.pumpAndSettle();
      print("Third test is starting");
      print("tapped button");

      TestAsyncUtils.guard(() async {
        await Future.delayed(Duration(seconds: 15));
      });
      await widgetTester.pumpAndSettle();
      print("Third test is finished");
    }, timeout: const Timeout(Duration(seconds: 10)));

    testWidgets("last test", (widgetTester) async {
      // This should get successful. but its not
      await widgetTester.pumpAndSettle();
      print("last test is run");
    });
  });
}

```
3. Add project dev_dependecies in pubspec.yaml:
```
  integration_test:
    sdk: flutter
  flutter_driver:
    sdk: flutter
```
4. Run following command
`flutter driver --target=integration_test/main.dart`

It will have errors.

### Expected results

The `last test` in integration_test should be independently run successful without getting affected from `third test` failure. 

### Actual results

The last test fails due to Timeout failure in Third test.

### Code sample

<details><summary>Integration test file</summary>

```dart
import 'dart:async';

import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import '../lib/main.dart' as app_main;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  group("All tests", () {
    setUpAll(() async {
      print("Setup all is run");
      app_main.main();
    });
    testWidgets("first test", (widgetTester) async {
      // This should get successful.
      await widgetTester.pumpAndSettle();
      print("First test is run");
    });
    testWidgets("second test", (widgetTester) async {
      // This should get successful.
      await widgetTester.pumpAndSettle();
      print("Second test is run");
    });
    testWidgets("third test", (widgetTester) async {
      // This will throw test error.
      await widgetTester.pumpAndSettle();
      print("Third test is starting");
      print("tapped button");

      TestAsyncUtils.guard(() async {
        await Future.delayed(Duration(seconds: 15));
      });
      await widgetTester.pumpAndSettle();
      print("Third test is finished");
    }, timeout: const Timeout(Duration(seconds: 10)));

    testWidgets("last test", (widgetTester) async {
      // This should get successful. but its not
      await widgetTester.pumpAndSettle();
      print("last test is run");
    });
  });
}

```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs from above project</summary>

```console

I/flutter (18171): 03:00 +1 -1: Home page tests test_desc-1 [E]
I/flutter (18171):   'package:flutter_test/src/binding.dart': Failed assertion: line 1960 pos 12: '!inTest': is not true.
I/flutter (18171):   dart:core-patch/errors_patch.dart 51:61                _AssertionError._doThrowNew
I/flutter (18171):   dart:core-patch/errors_patch.dart 40:5                 _AssertionError._throwNew
I/flutter (18171):   package:flutter_test/src/binding.dart 1960:12          LiveTestWidgetsFlutterBinding.runTest
I/flutter (18171):   package:integration_test/integration_test.dart 239:17  IntegrationTestWidgetsFlutterBinding.runTest
I/flutter (18171):   package:flutter_test/src/widget_tester.dart 158:24     testWidgets.<fn>
I/flutter (18171):   package:test_api/src/backend/declarer.dart 215:19      Declarer.test.<fn>.<fn>
I/flutter (18171):   ===== asynchronous gap ===========================
I/flutter (18171):   package:test_api/src/backend/declarer.dart 213:7       Declarer.test.<fn>
I/flutter (18171):   ===== asynchronous gap ===========================
I/flutter (18171):   package:test_api/src/backend/invoker.dart 258:9        Invoker._waitForOutstandingCallbacks.<fn>
I/flutter (18171):
I/flutter (18171):   'package:flutter_test/src/binding.dart': Failed assertion: line 1984 pos 12: '!_expectingFrame': is not true.
I/flutter (18171):   dart:core-patch/errors_patch.dart 51:61           _AssertionError._doThrowNew
I/flutter (18171):   dart:core-patch/errors_patch.dart 40:5            _AssertionError._throwNew
I/flutter (18171):   package:flutter_test/src/binding.dart 1984:12     LiveTestWidgetsFlutterBinding.postTest
I/flutter (18171):   dart:async/future.dart 305:31                     new Future.sync
I/flutter (18171):   package:test_api/src/backend/invoker.dart 236:18  Invoker.runTearDowns.<fn>.<fn>
I/flutter (18171):   package:test_api/src/backend/invoker.dart 258:17  Invoker._waitForOutstandingCallbacks.<fn>
I/flutter (18171):   dart:async/zone.dart 1399:13                      _rootRun
I/flutter (18171):   dart:async/zone.dart 1301:19                      _CustomZone.run
I/flutter (18171):   dart:async/zone.dart 1804:10                      _runZoned
I/flutter (18171):   dart:async/zone.dart 1747:10                      runZoned
I/flutter (18171):   package:test_api/src/backend/invoker.dart 254:5   Invoker._waitForOutstandingCallbacks
I/flutter (18171):   package:test_api/src/backend/invoker.dart 235:9   Invoker.runTearDowns.<fn>
I/flutter (18171):   dart:async/zone.dart 1399:13                      _rootRun
I/flutter (18171):   dart:async/zone.dart 1301:19
```

</details>

<details><summary>Other logs from my private project</summary>

```console

I/flutter (18171): 03:00 +1 -1: Home page tests test_desc-1 [E]
I/flutter (18171):   'package:flutter_test/src/binding.dart': Failed assertion: line 1960 pos 12: '!inTest': is not true.
I/flutter (18171):   dart:core-patch/errors_patch.dart 51:61                _AssertionError._doThrowNew
I/flutter (18171):   dart:core-patch/errors_patch.dart 40:5                 _AssertionError._throwNew
I/flutter (18171):   package:flutter_test/src/binding.dart 1960:12          LiveTestWidgetsFlutterBinding.runTest
I/flutter (18171):   package:integration_test/integration_test.dart 239:17  IntegrationTestWidgetsFlutterBinding.runTest
I/flutter (18171):   package:flutter_test/src/widget_tester.dart 158:24     testWidgets.<fn>
I/flutter (18171):   package:test_api/src/backend/declarer.dart 215:19      Declarer.test.<fn>.<fn>
I/flutter (18171):   ===== asynchronous gap ===========================
I/flutter (18171):   package:test_api/src/backend/declarer.dart 213:7       Declarer.test.<fn>
I/flutter (18171):   ===== asynchronous gap ===========================
I/flutter (18171):   package:test_api/src/backend/invoker.dart 258:9        Invoker._waitForOutstandingCallbacks.<fn>
I/flutter (18171):
I/flutter (18171):   'package:flutter_test/src/binding.dart': Failed assertion: line 1984 pos 12: '!_expectingFrame': is not true.
I/flutter (18171):   dart:core-patch/errors_patch.dart 51:61           _AssertionError._doThrowNew
I/flutter (18171):   dart:core-patch/errors_patch.dart 40:5            _AssertionError._throwNew
I/flutter (18171):   package:flutter_test/src/binding.dart 1984:12     LiveTestWidgetsFlutterBinding.postTest
I/flutter (18171):   dart:async/future.dart 305:31                     new Future.sync
I/flutter (18171):   package:test_api/src/backend/invoker.dart 236:18  Invoker.runTearDowns.<fn>.<fn>
I/flutter (18171):   package:test_api/src/backend/invoker.dart 258:17  Invoker._waitForOutstandingCallbacks.<fn>
I/flutter (18171):   dart:async/zone.dart 1399:13                      _rootRun
I/flutter (18171):   dart:async/zone.dart 1301:19                      _CustomZone.run
I/flutter (18171):   dart:async/zone.dart 1804:10                      _runZoned
I/flutter (18171):   dart:async/zone.dart 1747:10                      runZoned
I/flutter (18171):   package:test_api/src/backend/invoker.dart 254:5   Invoker._waitForOutstandingCallbacks
I/flutter (18171):   package:test_api/src/backend/invoker.dart 235:9   Invoker.runTearDowns.<fn>
I/flutter (18171):   dart:async/zone.dart 1399:13                      _rootRun
I/flutter (18171):   dart:async/zone.dart 1301:19
```
</details>

### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[!] Flutter (Channel [user-branch], 3.13.6, on macOS 13.2.1 22D68 darwin-arm64, locale en-IN)                                                                                                                             ok  18:20:30 
    ! Flutter version 3.13.6 on channel [user-branch] at /Users/vcgupta/Documents/Tools/flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
    • Framework revision ead455963c (3 months ago), 2023-09-26 18:28:17 -0700
    • Engine revision a794cf2681
    • Dart version 3.1.3
    • DevTools version 2.25.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/vcgupta/Library/Android/sdk
    • Platform android-34, build-tools 33.0.2
    • ANDROID_HOME = /Users/vcgupta/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] Android Studio (version 2022.2)
    • Android Studio at /Users/vcgupta/Desktop/Archived/Android Studio - Flamingo | 2022.2.1 Patch 1/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] IntelliJ IDEA Community Edition (version 2023.1.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] IntelliJ IDEA Community Edition (version 2023.1.2)
    • IntelliJ at /Users/vcgupta/Desktop/Archived/IntelliJ IDEA CE/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.84.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 13 (API 33) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 13.2.1 22D68 darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 120.0.6099.71

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

```

</details>