Migration to Flutter Guide
Discover our battle-tested 21-step framework for a smooth and successful migration to Flutter!
Home
Glossary

Golden tests in Flutter

What are golden tests in Flutter?

Golden tests in Flutter allow you to verify UI consistency by comparing rendered widgets against reference images. They are a powerful tool for catching visual regressions – but only if configured correctly. Without proper setup, golden tests quickly become flaky and unreliable, especially in CI/CD environments.

Golden tests render a widget and compare its visual output with a stored "golden" image. If even a single pixel differs, the test fails.

They are commonly used for:

  • design system components
  • reusable UI widgets
  • visual regression testing

Golden tests are not logic tests – they validate appearance, not behavior.

Platform differences: the biggest pitfall

Golden tests are platform-sensitive. The same widget can render slightly differently on macOS, Windows, and Linux due to:

  • font rendering differences
  • anti-aliasing
  • system font versions

This becomes a major issue when golden files are generated locally (often macOS) and validated in CI (usually Linux). Without proper tooling, this leads to flaky tests and false negatives.

While golden tests can be written using flutter_test, doing so directly is verbose and fragile. Alchemist is the modern, actively maintained standard for golden testing in Flutter. It builds on top of flutter_test and solves the most common golden test problems.

Why Alchemist is the preferred solution

Alchemist provides:

  • consistent font loading
  • deterministic rendering environments
  • clean APIs for composing multiple golden scenarios
  • support for multiple variants (themes, sizes, states) in a single test

Instead of writing many separate tests, you define golden scenarios that describe how a widget should look under different conditions. This makes golden tests easier to read, maintain, and review in pull requests.

Freezing dynamic content

Golden tests must be fully deterministic.

Before capturing a golden image:

  • Avoid DateTime.now() and random values.
  • Replace dynamic data with fixed constants.
  • Mock or disable network images.
  • Ensure animations are settled or disabled.

Any dynamic input will cause random test failures and invalidate the test's purpose.

Updating golden files

When a UI change is intentional, golden files must be regenerated manually.

Use:

flutter test --update-goldens

Always review visual diffs before committing updated goldens. Blindly updating goldens can hide real regressions.

Best practices for golden tests in Flutter

Following consistent practices helps ensure that golden tests remain reliable and produce stable visual comparisons.

  • Test small, isolated widgets.
  • Lock themes, fonts, and surface sizes.
  • Run golden tests on Linux in CI.
  • Keep test data deterministic.
  • Use Alchemist instead of raw flutter_test.

Common mistakes to avoid

Several common mistakes can make golden tests unstable or difficult to maintain. These are:

  • Generating goldens on one OS and validating on another without font control.
  • Testing highly dynamic widgets.
  • Treating golden tests as snapshot tests for entire screens.
  • Expecting Hot Reload to update golden files.

Golden tests are most effective when used sparingly and intentionally. Combined with Alchemist, they become a reliable guardrail against accidental UI regressions in Flutter projects.

Learn more

UI Testing in Flutter by LeanCode

The Role of UI Testing in Large-Scale Applications

We dive into the topic of UI testing in Flutter. Read more about automated UI tests' benefits and available solutions and why UI testing plays a crucial role, especially in large-scale applications.

Email Testing in Automated Tests - LeanCode

Mastering Email Testing in Automated End-to-End Tests

This article explores effective strategies for testing email-dependent flows in automated end-to-end tests. It covers practical methods from workarounds to tools along with their implementation, pros and cons, and best practices for seamless integration into development pipelines.

Testing SMS in Automated Tests by LeanCode

Testing SMS in Automated Tests for Optimal Delivery

Testing SMS often seems like a simple task - until you try to automate it. Each message depends on external networks, carriers, and devices, making even small tests unpredictable. This article explains how to choose the right approach for stable and reliable results.