Repository: flutter
Issue: Output of `print()` calls (made by the app under test) always lands in the first test when running integration tests run with `--reporter json` 
Labels: a: tests,tool,has reproducible steps,P2,found in release: 3.3,found in release: 3.4,team-tool,triaged-tool

## Synopsis

When running Flutter integration tests with the JSON reporter, `print()` calls that the app code is making have wrong `testID`.

This only occurs when running integration tests with `flutter test integraton_test`. Widget tests run with `flutter test` are not impacted.

## Steps to Reproduce

1. Clone the [bartekpacia/flutter_test_json_reporter_bug](https://github.com/bartekpacia/flutter_test_json_reporter_bug) repository. This is a default counter app slightly modified by me to demonstrate this problem. `cd` to the cloned repository.
3. `$ flutter pub get`
4. `$ flutter test integration_test/widget_test.dart --reporter json > reports/i_widget_test.jsonl`

**Actual results:**

The events of type `print`, messageType `print` have wrong `testID` – the events with output of all `print()` calls that the app under test is making have `testID` of the first test.

Grep `print()`s made by the first test.

```console
$ cat ./reports/i_widget_test.jsonl | grep "title increment"
{"testID":4,"messageType":"print","message":"build() called with title increment","type":"print","time":15889}
{"testID":4,"messageType":"print","message":"build() called with title increment","type":"print","time":16769}
```

Grep `print()`s made by the second test.

```console
$ cat ./reports/i_widget_test.jsonl | grep "title decrement"
{"testID":4,"messageType":"print","message":"build() called with title decrement","type":"print","time":17059}
{"testID":4,"messageType":"print","message":"build() called with title decrement","type":"print","time":17177
```

**Expected results:**

I expect `print()`s to have correct `testID`s. In the above example, that'd be:

```console
$ cat ./reports/i_widget_test.jsonl | grep "title decrement"
{"testID":5,"messageType":"print","message":"build() called with title decrement","type":"print","time":17059}
{"testID":5,"messageType":"print","message":"build() called with title decrement","type":"print","time":17177
```

Ignore `time` in the above logs – it doesn't matter.


<details>

  <summary>Code sample</summary>

  The code is available in the repository I linked above.

  See the [integration_test/widget_test.dart](https://github.com/bartekpacia/flutter_test_json_reporter_bug/blob/master/integration_test/widget_test.dart) file.

</details>

<details>
  <summary>Logs</summary>

  ```
  $ flutter doctor -v
  [✓] Flutter (Channel stable, 3.3.5, on macOS 12.6 21G115 darwin-arm, locale en-PL)
      • Flutter version 3.3.5 on channel stable at /Users/bartek/fvm/versions/stable
      • Upstream repository https://github.com/flutter/flutter.git
      • Framework revision d9111f6402 (3 days ago), 2022-10-19 12:27:13 -0700
      • Engine revision 3ad69d7be3
      • Dart version 2.18.2
      • DevTools version 2.15.0

  [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
      • Android SDK at /Users/bartek/androidsdk
      • Platform android-33, build-tools 33.0.0
      • ANDROID_HOME = /Users/bartek/androidsdk
      • Java binary at: /Users/bartek/Library/Application
        Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android
        Studio.app/Contents/jre/Contents/Home/bin/java
      • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
      • All Android licenses accepted.

  [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
      • Xcode at /Applications/Xcode.app/Contents/Developer
      • Build 14A400
      • CocoaPods version 1.11.3

  [✓] Chrome - develop for the web
      • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

  [✓] Android Studio (version 2021.3)
      • Android Studio at /Users/bartek/Library/Application
        Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/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 11.0.13+0-b1751.21-8125866)

  [✓] IntelliJ IDEA Ultimate Edition (version 2022.2.3)
      • IntelliJ at /Users/bartek/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
      • Flutter plugin version 70.2.5
      • Dart plugin version 222.4345.14

  [✓] IntelliJ IDEA Ultimate Edition (version 2022.2.3)
      • IntelliJ at /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/222.4345.14/IntelliJ
        IDEA.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.72.2)
      • VS Code at /Applications/Visual Studio Code.app/Contents
      • Flutter extension version 3.50.0

  [✓] Connected device (3 available)
      • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 13 (API 33) (emulator)
      • macOS (desktop)             • macos         • darwin-arm64   • macOS 12.6 21G115 darwin-arm
      • Chrome (web)                • chrome        • web-javascript • Google Chrome 106.0.5249.119

  [✓] HTTP Host Availability
      • All required HTTP hosts are available

  • No issues found!
  ```

</details>


### Better visualization of the JSON test results

To better visualize the test output, I suggest converting the test results from `flutter test --reporter json`'s format to JUnit format using the [junitreport](https://pub.dev/packages/junitreport) package and then convert the JUnit `.xml` file to a simple HTML website using the Python [junit2html](https://github.com/inorton/junit2html) package.

1. Install the `junitreport` package:

    ```console
    $ dart pub global activate junitreport
    ```

2. (Assuming you have Python installed) Install the `junit2html` package:

    ```console
    $ pip install junit2html
    ```

3. Run the integration tests with the JSON reporter

    ```console
    $ flutter test integration_test/widget_test.dart --reporter json > reports/i_widget_test.jsonl
    ```

4. Convert `flutter test` output to JUnit format

    ```console
    $ dart pub global run junitreport:tojunit --input reports/i_widget_test.jsonl --output reports/i_widget_test.xml
    ```

5. Generate nice HTML report of the test output from the JUnit format

    ```console
    $ junit2html reports/widget_test.xml reports/widget_test.html
    ```

6. Open `reports/widget_test.html` with the browser of your choice. You should see this website. Here it can be very clearly seen that the stdout is messed up.

    ![Screenshot 2022-10-22 at 1 49 50 PM](https://user-images.githubusercontent.com/40357511/197337387-57e1bd0a-b1b9-4294-b15b-330ad13ff9f4.png)


    For comparison, see the output of `flutter test test/widget_test --reporter json` (converted to JUnit and then converted to HTML report). This is the correct behavior.

    ![Screenshot 2022-10-22 at 1 54 19 PM](https://user-images.githubusercontent.com/40357511/197337564-fee7cc40-22a0-4e1d-a716-f9d3b179468f.png)