Skip to content

NetworkImage in tests causes spurious warning about HttpClient #129532

@gnprice

Description

@gnprice

Currently if you write a test case that uses a NetworkImage, the test framework will generate an warning saying that your test created an HttpClient, and that this can cause tests to fail.

This happens regardless of whether there's any actual use of HttpClient that could affect the tests — in particular it happens even if you're correctly using debugNetworkImageHttpClientProvider to get NetworkImage to use a fake HttpClient. That makes it a source of confusion and of time spent debugging down a wrong direction.

The flutter test runner will only actually print the warning if the test ends up failing. Partly this is good, because it mitigates the noise the warning would otherwise add to the output… but it also aggravates the problem of the confusing debugging signal, because it looks like the warning is correlated with the test failure.

Repro steps

  1. Run flutter test on the sample code: flutter test test/a_test.dart.

Expected results

The test fails, without spurious additional warnings not related to the failure.

Actual results

The test fails, with a warning about HttpClient:

$ flutter test
00:01 +0 -1: some test involving a NetworkImage [E]                                            
  Expected: <3>
    Actual: <2>
  
  package:matcher                                     expect
  package:flutter_test/src/widget_tester.dart 467:18  expect
  test/a_test.dart 19:5                               main.<fn>
  
Warning: At least one test in this suite creates an HttpClient. When
running a test suite that uses TestWidgetsFlutterBinding, all HTTP
requests will return status code 400, and no network request will
actually be made. Any test expecting a real network connection and
status code will fail.
To test code that needs an HttpClient, provide your own HttpClient
implementation to the code under test, so that your test can
consistently provide a testable response to the code under test.

To run this test again: /home/greg/n/flutter/flutter/bin/cache/dart-sdk/bin/dart test /home/greg/n/flutter/scratch/test/a_test.dart -p vm --plain-name 'some test involving a NetworkImage'
00:01 +0 -1: Some tests failed.                                                                

Sample code

// test/a_test.dart
import 'dart:io';

import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  test("some test involving a NetworkImage", () {
    // Properly mock out [NetworkImage]'s use of [HttpClient].
    debugNetworkImageHttpClientProvider = () => _FakeHttpClient();

    // Then use [NetworkImage].
    NetworkImage(nonconst('https://example/foo.png')).resolve(ImageConfiguration.empty);

    // (Imagine some longer, more complicated, test code here.)

    // Then the test fails, for reasons unrelated to [HttpClient] or even [NetworkImage].
    expect(1 + 1, 3);

    // Under the failure message, we get a spurious warning about [HttpClient].
  });
}

class _FakeHttpClient extends Fake implements HttpClient {}

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: error messageError messages from the Flutter frameworka: imagesLoading, displaying, rendering imagesa: tests"flutter test", flutter_test, or one of our testsframeworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions