Reputation: 8230
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
Reputation: 8230
I came up with my own solution:
I created a file test/flutter_test_config.dart
which is a special dart file for configuring the tests (see flutter_test
).
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).
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
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