Flutter Image Test Utils 🖼️

This package provides a simple and easier way to test flutter widgets that are using `Image.network` or `CachedNetworkImage`, by using a mock HTTP client with mocktail stubs that responds with a fake image for every requests.

Drop a 👍🏽 and a ⭐ to support the project!

Note

This is an inspiration of: image_test_utils that is discontinued and mock_image_http from the Flutter team.

Important

On the web, this package does not mock the HTTP client. It only executes the callback. This is because dart:io is not available on the web.

Usage

Run this command on the root folder of your Flutter project:

flutter pub add -d flutter_image_test_utils

Or simple paste the following dependency on your pubspec.yaml file:

dev_dependencies:
  flutter_image_test_utils: ^1.1.0

Import the library and wrap your test function with: provideMockedNetworkImages(), for example:

import ...
import 'package:flutter_image_test_utils/flutter_image_test_utils.dart';

testWidgets('should do something', (tester) async {
  provideMockedNetworkImages(
    () async {
      await tester.pumpWidget(
        MaterialApp(
          home: MyWidget(),
        ),
      );
      ...
    },
  );
});

You can also use your own image bytes value, you just need to pass it to the function with the imageBytes parameter:

import ...

testWidgets('should do something', (tester) async {
  provideMockedNetworkImages(
    () async {
      ...
    },
    imageBytes: [ ... ]
  );
});