— Flutter, Dart, Automation, GitHub Actions — 1 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.
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:
pub get
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 branch6 pull_request:7 branches: [master]8
9env:10 flutter_version: "2.2.3"11
12jobs:13 run_tests:14 runs-on: ubuntu-latest15
16 steps:17 - uses: actions/checkout@v218 # Cache Flutter environment19 - name: Cache Flutter dependencies20 uses: actions/cache@v221 with:22 path: /opt/hostedtoolcache/flutter23 key: ${{ runner.OS }}-flutter-install-cache-${{ env.flutter_version }}24 - uses: subosito/flutter-action@v125 with:26 flutter-version: ${{ env.flutter_version }}27 channel: stable28 # Run pub get29 - name: Run pub get30 run: flutter pub get31 # Runs tests32 - name: Run tests33 run: flutter test
master
branch. You could change this to whatever your default branch is, you can even define multiple branches.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).2.x
for the version(but tossed out caching) so I don't have to update my actions every couple of weeks.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.