-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(flutter): add Snapshots setup guide #18822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lucas-zimerman
wants to merge
3
commits into
master
Choose a base branch
from
lz/flutter/snapshot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| title: Set Up Snapshots | ||
| sidebar_title: Snapshots | ||
| sidebar_order: 5300 | ||
| sidebar_section: features | ||
| description: Set up snapshots for Flutter apps using golden file tests. | ||
| beta: true | ||
| --- | ||
|
|
||
| <Include name="feature-stage-beta.mdx" /> | ||
|
|
||
| Set up [Snapshots](/product/snapshots/) for your Flutter app using Flutter's built-in golden file testing. Generate snapshots locally or in your own CI, then upload the generated images to Sentry for image diffing, visual review, and GitHub status checks. | ||
|
|
||
| ## Step 1: Generate Images | ||
|
|
||
| Sentry only needs a PNG or JPEG image of each screen, so any tool that produces one works. This guide uses Flutter's built-in `flutter_test` package, which can render your widgets and save the result as an image using `matchesGoldenFile`. Write a test for each screen or component you want to snapshot: | ||
|
|
||
| ```dart | ||
| void main() { | ||
| group('snapshot', () { | ||
| testWidgets('renders home screen', (tester) async { | ||
| await tester.pumpWidget(const MaterialApp(home: MyHomeScreen())); | ||
|
|
||
| await expectLater( | ||
| find.byType(MyHomeScreen), | ||
| matchesGoldenFile('home_screen.png'), | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
| ``` | ||
|
|
||
| Put your snapshot tests in a dedicated directory, such as `test/snapshots/`, so you can generate and upload them independently of your other widget tests. By default, `matchesGoldenFile` writes the image next to the test file that calls it, so a test at `test/snapshots/home_screen_test.dart` produces `test/snapshots/home_screen.png`. | ||
|
|
||
| Then generate the images by running your tests with `--update-goldens`: | ||
|
|
||
| ```bash | ||
| flutter test --update-goldens test/snapshots/ | ||
| ``` | ||
|
|
||
| <Alert level="info"> | ||
|
|
||
| `--update-goldens` only writes images to disk — it doesn't compare them against a baseline. Sentry, not `flutter test`, performs the visual diffing, so you don't need to commit the generated `.png` files to your repository. | ||
|
|
||
| </Alert> | ||
|
|
||
| <Alert level="warning"> | ||
|
|
||
| Widget tests render text with a placeholder test font unless you load your app's real fonts first, so text may appear as solid blocks in your snapshots. Load your fonts in a [`flutter_test_config.dart`](https://api.flutter.dev/flutter/flutter_test/) file, or use a helper like [`golden_toolkit`'s `loadAppFonts()`](https://pub.dev/packages/golden_toolkit), before running your snapshot tests. | ||
|
|
||
| </Alert> | ||
|
|
||
| Any tool that writes PNG or JPEG files works with Sentry, including packages built on top of `matchesGoldenFile` like [`golden_toolkit`](https://pub.dev/packages/golden_toolkit) and [`alchemist`](https://pub.dev/packages/alchemist), which add support for multi-device and multi-theme goldens. | ||
|
|
||
| ## Step 2: Test Locally | ||
|
|
||
| Generate the images, then upload them with `sentry-cli`: | ||
|
|
||
| ```bash | ||
| flutter test --update-goldens test/snapshots/ | ||
|
|
||
| sentry-cli snapshots upload --app-id com.example.your-app test/snapshots/ | ||
| ``` | ||
|
|
||
| See [Uploading Snapshots](/product/snapshots/uploading-snapshots/) for the full CLI reference and metadata schema. | ||
|
|
||
| ## Step 3: Integrate Into CI | ||
|
|
||
| Once the local upload succeeds, wire the same commands into your CI. See [Integrating Into CI](/product/snapshots/integrating-into-ci/) for the general GitHub Actions structure. | ||
|
|
||
| ## Best Practices | ||
|
|
||
| - **Run golden tests in a consistent environment.** Flutter renders text and shapes using the host operating system's font and graphics stack, so the same test can produce different pixels on macOS, Linux, and Windows. Generate your baseline and CI snapshots on the same OS (matching your CI runner locally with something like a Linux container) to avoid spurious diffs. | ||
| - **Pin your Flutter SDK version.** Rendering can change between Flutter versions. Use the same version locally and in CI to keep snapshots stable. | ||
| - **Isolate snapshot tests.** Put them in a dedicated directory so you can generate and upload them independently of your other widget tests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.