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:
- Checkout the project
- Caching(optional because it only saves around 30 seconds or so)
- Install Flutter
- Run
pub get
- Run the tests
The file for the action
So with the steps written above this is what we roughly end up with:
name: Run Tests(for PRs)
on:
workflow_dispatch:
# Runs when a PR is made against master branch
pull_request:
branches: [ master ]
env:
flutter_version: "2.2.3"
jobs:
run_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Cache Flutter environment
- name: Cache Flutter dependencies
uses: actions/cache@v2
with:
path: /opt/hostedtoolcache/flutter
key: ${{ runner.OS }}-flutter-install-cache-${{ env.flutter_version }}
- uses: subosito/flutter-action@v1
with:
flutter-version: ${{ env.flutter_version }}
channel: stable
# Run pub get
- name: Run pub get
run: flutter pub get
# Runs tests
- name: Run tests
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.
Top comments (2)
Hi, I like your website. I added this article to my weekly newsletter.
I do not use GitHub actions much, but if I ever need them in the future, I know where to look.
Hey Michele,
Sorry for the late reply. Thanks for adding this article to your newsletter! I also noticed you added something else I wrote the other week I think(it was about mixing null safety packages), thanks for that too.
The newsletter is great by the way! I will definitely subscribe.