I have an existing flutter application, that I would like to configure with a couple of simple unit tests. I followed instructions from https://codelabs.developers.google.com/codelabs/flutter-app-testing#0 with pointers on how to configure a project with unit tests, and applied those steps to my own project.
After applying those steps flutter test is not behaving like how I would expect, and blatantly just doesn't work. In specific, it yields the following output Connection closed before test suite loaded.
While trying to resolve the error a couple of oddities:
When I ran unit tests (from vscode) I noticed that the application was "paused" on an unhandled exception.
When I looked into the exception, it was about a dependency my actual application relies on. Specifically, about SQLite not being able to find the .so/.dll
When I looked into the callstack, I noticed that it was basically just loading my actual application instead of the unit tests own entry point. Specifically, it was loading all of my app providers.
After I removed that specific exception, it would continue to fail on other parts. For example about unhandled exceptions over missing assets. This is something I could work around with
--[no-]test-assetsto force the compilation of assets.
The behavior I have witnessed above. I sort of came to a conclusion that it's trying to execute the actual application instead of the tests altogether (hence the various missing dependencies etc). So as a theory, I removed the main function from my main.dart. Running flutter test then fails to compile some autogenerated generated_main.dart, because the entrypoint is missing.
//
// 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 brevity
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)();
}
}
From what I observed, most of the issues seem to come from the fact that's choosing to run my actual application instead of the unit test. That first line import 'package:liquid/main.dart' as entrypoint; is basically referencing the application entrypoint.
According to https://github.com/flutter/flutter/issues/8516 there used to be a time where flutter test wasn't supported but it's already been closed. So I assume there are no fundamental broken issues "just because windows platform".
However, it appears that something is not completely right on my setup and I am not really sure what I need to do to get a simple test case working.