How to run Flutter Golden (Snapshot) Tests with Codemagic CI/CD — Part2

Katarina Sheremet
May 26 · 4 min read
Image for post
Image for post
Photo by Christopher Gower on Unsplash

In my previous article, I’ve covered generating Flutter Golden Snapshots using Codemagic, downloading them from the artifacts section, and adding them to your project. This is a follow-up article where I’ll explain how to send a Pull Request with updated Flutter Golden Snapshots using Codemagic. Let’s begin …

Step 1: Set up Build Trigger

The project has a specific Codemagic workflow for generating golden files. We’d like to trigger this workflow only when we need to update golden files. Therefore let’s create a Build trigger, where the workflow will run, given that the git branch has the word “golden” in its name.

Image for post
Image for post

Step 2: Check the Test section

We need to make sure that Flutter tests are enabled and Flutter test arguments section runs the following command:

Image for post
Image for post

Step 3: Update the Post-Script

In the previous article, we manually downloaded golden snapshots from the artifacts and copied them to the project. This time, let’s automatically update golden snapshots and push them back to the repository.

We’ll run the following script in the Post-Script section:

In this script, we add updated golden files to the working directory. If (and only if) the goldens were changed, we commit the changes and push them to the same branch. If we run ‘git commit’ when no changes were made, the command will fail.

Image for post
Image for post

Codemagic allows configuring the workflow for Flutter apps using UI. But you can also use the YAML configuration file, which has to be committed to the repository. My workflow “generate-goldens” looked like this:

Image for post
Image for post

Step 4: Working with Git

To make sure that the workflow generate-goldens triggers and golden files will be generated, we need to have a branch that contains the word “golden”. In my project folder I’ve used the following command to create a new branch:

After making and committing changes, I’ve pushed it to the repository.

In the Codemagic console I could immediately see that generate-goldens was triggered:

Image for post
Image for post

In a minute, a new commit with a generated golden file was added to the update-goldens brach.

Image for post
Image for post
Image for post
Image for post

You can see that the author of this commit was Builder. This name can be changed. In the Codemagic console, you can click on every section to see the build log. If you click on the Post-test script section, it gives instructions on how to change the author.

Image for post
Image for post

Useful links:

Flutter Community

Articles and Stories from the Flutter Community

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store