Skip to content

Widget tests overriding HTTP to return 400 is incredibly confusing and unintuitive #35318

@tomgilder

Description

@tomgilder

I’ve just lost several hours of debugging to this issue.

If you have a test file like this, the HTTP test passes:

import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';

void main() {
   test("HTTP request", () async {
    final result = await get(Uri.http("flutter.dev", "/"));
    expect(result.statusCode, 200);
  });
}

But adding even an empty widget test in the same file causes the HTTP test to fail - even if you just run the HTTP test:

import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';

void main() {
   test("HTTP request", () async {
    final result = await get(Uri.http("flutter.dev", "/"));
    expect(result.statusCode, 200);
  });

  testWidgets("Empty widget test", (tester) async { });
}

There’s nothing in the log or the faked HTTP response to indicate that Flutter in intercepting the response. It’s deeply unintuitive that just the presence of testWidgets - without the widget test even being run - would cause HTTP intercepting.

I also can’t find anything in the documentation about the intercepting.

Suggestions:

  • Don’t do the HTTP intercepting by default - make it so developers explicitly enable it
  • If that’s too much of a breaking change, at the very least document it
  • If possible, log that the HTTP interception is enabled
  • Add a message to the faked HTTP response explicitly saying it’s a fake response, why it’s been faked, and how to disable it

Metadata

Metadata

Assignees

Labels

a: error messageError messages from the Flutter frameworka: tests"flutter test", flutter_test, or one of our testscustomer: crowdAffects or could affect many people, though not necessarily a specific customer.d: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions