Skip to content
DeveloperMemos

Run Flutter tests with GitHub Actions

Flutter, Dart, Automation, GitHub Actions1 min read

If you're like me you probably like setting your project up to automatically run your tests when you create a Pull Request on GitHub. I wrote another post about upgrading Flutter packages with GitHub Actions in May but I realised today that I never wrote a post about running tests, so here we go.

Run Flutter tests with GitHub Actions

There isn't an Action on the marketplace that does all of the steps for you. It's still pretty easy to set things up but, for installing Flutter I always use subosito/flutter-action@v1. Here are the rough steps that I want my action to perform:

  1. Checkout the project
  2. Caching(optional because it only saves around 30 seconds or so)
  3. Install Flutter
  4. Run pub get
  5. Run the tests

The file for the action

So with the steps written above this is what we roughly end up with:

1name: Run Tests(for PRs)
2
3on:
4 workflow_dispatch:
5 # Runs when a PR is made against master branch
6 pull_request:
7 branches: [master]
8
9env:
10 flutter_version: "2.2.3"
11
12jobs:
13 run_tests:
14 runs-on: ubuntu-latest
15
16 steps:
17 - uses: actions/checkout@v2
18 # Cache Flutter environment
19 - name: Cache Flutter dependencies
20 uses: actions/cache@v2
21 with:
22 path: /opt/hostedtoolcache/flutter
23 key: ${{ runner.OS }}-flutter-install-cache-${{ env.flutter_version }}
24 - uses: subosito/flutter-action@v1
25 with:
26 flutter-version: ${{ env.flutter_version }}
27 channel: stable
28 # Run pub get
29 - name: Run pub get
30 run: flutter pub get
31 # Runs tests
32 - name: Run tests
33 run: flutter test

Tinkering

  • The above action will run whenever you create a Pull Request against the master branch. You could change this to whatever your default branch is, you can even define multiple branches.
  • I also added workflow_dispatch so the action can be run manually - the reason I did this is because the cache doesn't seem to build properly unless you run the action on the base branch once(this seems to be by design according to GitHub).
  • You can also change the Flutter version around to your liking, the action above is pinned to 2.2.3 which is the latest stable version at this time. In some of my projects I started specifying 2.x for the version(but tossed out caching) so I don't have to update my actions every couple of weeks.

Potential Improvements

I think the only thing you could potentially add to the above action is something to build the project at the end like flutter build ios or flutter build apk. If your unit tests don't have great coverage you could potentially break your project and I'm pretty sure your tests would still run without any issues. Adding a build step to the end would make sure that your project still builds correctly, but keep in mind this would make the action take longer to finish.