Automate Flutter Testing With Github Actions

Flutter and Github Actions Logo

Overview

When working on a large team, or open-source project, code reviews can get really time-consuming, especially when you have to read through tons of poorly written code. One way to take some of this weight off your back is to set up automated code analysis, formatting, and UI tests.

Prerequisites

Steps

Create a Flutter App in the root directory of the Github repository

flutter create --project-name myapp .
analyzer:
exclude:
- flutter/**

Create the Github Action

Github Actions are stored in the .github/workflows folder, so for the tests create a file called: .github/workflows/tests.yaml.

Parts:

Name

The Name is the first part of a Github Actions file, it defines the name of the actions in the file.

name: TEST FLUTTER APP

On

The On section defines when to run the action. This action needs to run whenever a pull request is made, in order to verify the code in it.

on:
push:
pull_request:
branches:
- main

Env

The Env defines the global environment variables for the actions. To make this easily updatable, the Flutter version should be put in an environment variable.

env:
FLUTTER_VERSION: "1.22.1"

Jobs

The Jobs section defines the jobs that will be completed. The Flutter Tests Job looks like this:

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Flutter
run: git clone https://github.com/flutter/flutter.git --depth 1 -b $FLUTTER_VERSION flutter
- name: Add Flutter to Path
run: echo "$GITHUB_WORKSPACE/flutter/bin" >> $GITHUB_PATH
- name: Install Dependencies
run: flutter pub get
- name: Flutter Analyze
run: flutter analyze --no-pub
- name: Check Flutter Formatting
run: flutter format lib/** --set-exit-if-changed
- name: Run Flutter Tests
run: flutter test --no-pub
  1. Install Flutter gets the Flutter code from the Flutter Repository on Github
  2. Adds Flutter to the Path of the Github Action, so it can use the flutter cli
  3. Installs the dependencies of the app
  4. Runs flutter analyze to analyze the code and check for mistakes like unused imports
  5. Runs flutter format lib/** --set-exit-if-changed to check if the code is formatted properly. This will throw an error if the code is formatted badly
  6. Runs flutter test to run the tests defined in your app’s test directory

All Together

name: TEST FLUTTER APPon:
push:
pull_request:
branches:
- main
env:
FLUTTER_VERSION: "1.22.1"
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Flutter
run: git clone https://github.com/flutter/flutter.git --depth 1 -b $FLUTTER_VERSION _flutter
- name: Add Flutter to Path
run: echo "$GITHUB_WORKSPACE/_flutter/bin" >> $GITHUB_PATH
- name: Install Flutter Dependencies
run: flutter pub get
- name: Flutter Analyze
run: flutter analyze --no-pub
- name: Check Flutter Formatting
run: flutter format lib/** --set-exit-if-changed
- name: Run Flutter Tests
run: flutter test --no-pub

Automated Testing In Action

When someone makes a bad Pull Request:

Poorly written code fails the tests
Well written code passes the tests

Closing

These automated tests can help keep code quality high and lower your work. Hopefully, this helps you in your future flutter projects.

Aspiring Developer