Skip to content

Flutter test not working trying to run the application entrypoint, instead of those of test fixtures #88632

Closed

Description

@phr34k

I followed instructions from https://codelabs.developers.google.com/codelabs/flutter-app-testing#0 to introduce unit tests into my application. In short, the test always fails due to a generic Connection closed before test suite loaded. error.

I noticed that running the tests was failing on unhandled exceptions due to all sorts of (self-written) states/providers that were trying to initialize. Providers that application (normally) relies on, but definitely should have been out of the scope of the unit test. The call stack gave me the first impression that it was initializing my application (instead of the test).

The build pipeline appears to inject a file called generated_main.dart. This file, seems to import an entry-point of the application package, and then runs that from its own main function. I get the impression that the main function in this file end's up being the actual entry point used by the executable.

//
// Generated file. Do not edit.
// This file is generated from template in file `flutter_tools/lib/src/flutter_plugins.dart`.
//

// @dart = 2.6

import 'package:liquid/main.dart' as entrypoint;
import 'dart:io'; // flutter_ignore: dart_io_import.
import 'package:path_provider_linux/path_provider_linux.dart';
import 'package:path_provider_windows/path_provider_windows.dart';

... //stripped for brievity

typedef _UnaryFunction = dynamic Function(List<String> args);
typedef _NullaryFunction = dynamic Function();

void main(List<String> args) {
  if (entrypoint.main is _UnaryFunction) {
    (entrypoint.main as _UnaryFunction)(args);
  } else {
    (entrypoint.main as _NullaryFunction)();
  }
}

For me, line 8 of this file, always contains import 'package:liquid/main.dart' as entrypoint;. This is the entry point of the application, and not the unit-test. I asserted this idea with a simple validation, if I compile unit tests while I uncomment the main function in main.dart. it gave me a compile error.

I couldn't verify if anything that I observed above was done intentionally by design, it just seems very unlikely. According to #8516 flutter test should have been supported under windows.


flutter doctor -v
[√] Flutter (Channel dev, 2.5.0-6.0.pre, on Microsoft Windows [Version 10.0.19043.1165], locale en-US)
    • Flutter version 2.5.0-6.0.pre at E:\SDK\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 225a43d941 (2 weeks ago), 2021-08-05 11:32:48 -0700
    • Engine revision 07ec4b82c7
    • Dart version 2.14.0 (build 2.14.0-383.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at E:\SDK\Android\SDK
    • Platform android-30, build-tools 30.0.3
    • Java binary at: E:\SDK\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.9.31205.134
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 4.2)
    • Android Studio at E:\SDK\Android\Android Studio
    • 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.8+10-b944.6842174)

[√] VS Code (version 1.59.1)
    • VS Code at C:\Users\phr34k\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.25.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19043.1165]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 92.0.4515.131
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 92.0.902.73

• No issues found!

Activity

  1. stuartmorgan-g commented on Aug 21, 2021

    @stuartmorgan-g
    Contributor

    Yes, this is a bug that wasn't noticed until recently; see also #87991 (comment)

    In the short term you can work around it by deleting that generated main before running flutter test.

    /cc @blasten

  2. added
    a: buildBuilding flutter applications with the tool
    a: tests"flutter test", flutter_test, or one of our tests
    P1High-priority issues at the top of the work list
    platform-linuxBuilding on or for Linux specifically
    platform-macosBuilding on or for macOS specifically
    platform-windowsBuilding on or for Windows specifically
    c: regressionIt was better in the past than it is now
    toolAffects the "flutter" command-line tool. See also t: labels.
    on Aug 21, 2021
  3. stuartmorgan-g commented on Oct 1, 2021

    @stuartmorgan-g
    Contributor

    This should be fixed by #90288

  4. github-actions commented on Oct 15, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  5. locked as resolved and limited conversation to collaborators on Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority issues at the top of the work lista: buildBuilding flutter applications with the toola: tests"flutter test", flutter_test, or one of our testsc: regressionIt was better in the past than it is nowplatform-linuxBuilding on or for Linux specificallyplatform-macosBuilding on or for macOS specificallyplatform-windowsBuilding on or for Windows specificallytoolAffects the "flutter" command-line tool. See also t: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions