Automated Unit Testing with GitHub Actions

Search for a command to run...

No comments yet. Be the first to comment.
Here we will cover various aspects of unit, widget and integration testing in Flutter
The Problem… Often times within our applications, both on the UI and Data sides we will need to set “defaults” for dates, the simplest way to do that in most cases would be to use DateTime.now(), after all, we are most likely going to use the current...
The Problem Wasn’t Triage Most AI workflows in software engineering still keep the human directly in the middle of triage. The AI might help write code. It might explain a stack trace. It might summar

There’s been a steady undercurrent of people switching tools lately. Copilot to Claude. Claude to something else. Codex quietly entering more workflows. A lot of confidence in different directions, of

It’s been said to me that at times my responses can come across as a “no” first-unintentionally, of course, but real all the same. Not long ago, someone on my team shared an idea they were exploring. I began with a caution, meaning to help them avoid...

The opening sentence of any message often carries more weight than everything that follows. I’ve learned this the hard way. Recently, a report shared something he wanted to pursue. I was supportive — I really was — but my first response came as a cau...

One of the things I’ve always been comfortable with is admitting when I don’t know something. To me, it’s one of the real distinctions between the more and less experienced engineers — and honestly, people in general. When you’re earlier in your care...

Today we going to discuss the nicely simple way in which you can use GitHub's actions to automate the unit testing for your Flutter project. This can be very handy to prevent broken code from going to production, or even just as a sanity check for the code reviews.
Firstly, let's get a look at what our result is going to look like...
name: Flutter Testing
on:
workflow_dispatch:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.4
- uses: subosito/flutter-action@v1.5.3
- name: Install packages
run: flutter pub get
- name: Run generator
run: flutter pub run build_runner build
- name: Run test
run: flutter test test
Now let's dive into what is going on here.
First, we simply start off with a name and the triggers, this action will trigger for any pull-request made against the main branch as well as on "workflow_dispatch", which is GitHub for manually.
The jobs section is where the actual work happens.
GitHub exposes Linux, Windows and macOS environments for you to run actions within, as unit tests are platform agnostic, I prefer to run these against Linux, in part is it is the "cheapest" option, while for OpenSource projects you have unlimited minutes, private projects are collectively limited to 2000 minutes per month, with time multipliers based on the platform being used, you can read up about that HERE.
The steps describe the order of flow for your action, which almost always will start with actions/checkout@v2.3.4, which is the action that checks out the code into the instance of the action.
As we are trying to test a Flutter project, we need to install flutter into the instance, for this we are calling subosito/flutter-action@v1.5.3, but default this will install the latest stable release, but you can configure this to another release or even pin it to a specific version.
Next, we start interacting with our own code, we start by running flutter pub get so that we can install all our packages into the instance.
The "Run Generator" step is an optional one for those of us who make use of code generation in our projects, something you can skip if you are not already running it locally.
Lastly, we run our tests with flutter test test, while the 2nd "test" is not required, it is actually the path reference to the test folder, something I need to define in order for some of the mocking functions to work correctly within a few of my projects.
Yes, it really is that simple, that is all you need to be able to run your automated tests using GitHub actions, how long it takes would depend on the size of your project. My smallest project which is about a handful of files takes just over a minute, one of my largest was thousands of files with just over 1k test over 12k LOC and 80% coverage took about 9 minutes.
I hope you found this interesting, and if you have any questions, comments, or improvements, feel free to drop a comment. Enjoy your Flutter development journey :D
If you liked it, a heart would be awesome, and if you really liked it, a cup of coffee would be great.
Thanks for reading.