Valentin Vignal
Valentin Vignal

Reputation: 8230

How to disable checks on golden images in flutter test

I am trying to create a CI workflow on a Flutter project. In these workflows, I have to run the tests with flutter test. For some technical reasons, I would like to be able in some workflow to run the test with the golden images, and some others, run the tests while ignoring all the goldens images checks.

Is there a way to do so?

Upvotes: 2

Views: 1277

Answers (2)

Valentin Vignal
Valentin Vignal

Reputation: 8230

I came up with my own solution:

  1. I created a file test/flutter_test_config.dart which is a special dart file for configuring the tests (see flutter_test).

  2. I created a copy of TrivialComparator from flutter_test that always returns success for golden comparison. I couldn't reuse TrivialComparator as the constructor is private (GitHub).

  3. I used bool.fromEnvironment to be able to disable or not the goldens checks from the command line.

import 'dart:async';
import 'dart:typed_data';

import 'package:flutter_test/flutter_test.dart';

/// A copy of [TrivialComparator] from flutter_test that returns a
/// trivial success.
class TrivialGoldenFileComparator implements GoldenFileComparator {
  const TrivialGoldenFileComparator();

  static const ignoreGoldens = bool.fromEnvironment('ignore-goldens');

  @override
  Future<bool> compare(Uint8List imageBytes, Uri golden) {
    return Future<bool>.value(true);
  }

  @override
  Future<void> update(Uri golden, Uint8List imageBytes) {
    throw StateError('Golden files should not be updated while ignored.');
  }

  @override
  Uri getTestUri(Uri key, int? version) {
    return key;
  }
}


Future<void> testExecutable(FutureOr<void> Function() testMain) async {
  if (TrivialGoldenFileComparator.ignoreGoldens) {
    goldenFileComparator = const TrivialGoldenFileComparator();
  }

  await testMain();
}

If I run

flutter test

The goldens are checked.

If I run

flutter test --dart-define="ignore-goldens=true"

the goldens are ignored.

Upvotes: 1

patmuk
patmuk

Reputation: 21

You can with the highly recommended golden toolkit.

As described in this answer, you simply create a file in test/flutter_test_config.dart with the following content:

import 'dart:async';
import 'dart:io';

import 'package:golden_toolkit/golden_toolkit.dart';

Future<void> testExecutable(FutureOr<void> Function() testMain) async {
  return GoldenToolkit.runWithConfiguration(
    () async {
      await loadAppFonts();
      await testMain();
    },
    config: GoldenToolkitConfiguration(
      // Currently, goldens are not generated/validated in CI for this repo.     We have settled on the goldens for this package
      // being captured/validated by developers running on MacOSX. We may revisit this in the future if there is a reason to invest
      // in more sophistication
      skipGoldenAssertion: () => !Platform.isMacOS,
    ),
  );
}

Upvotes: 2

Related Questions