Automate Flutter Testing With Github Actions

Benjamin Swerdlow
3 min readDec 22, 2020

--

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.

This tutorial will go through how to setup Github Actions to check code quality, formatting, and run UI tests to validate pull requests.

All code in this repository will be available in template form here: https://github.com/theswerd/flutter-githubactions-testing-template

Prerequisites

Steps

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

flutter create --project-name myapp .

Create a file called analysis_options.yaml. This file will be used to customize the analyzer to make it ignore the flutter dependency installed in the actions.

This should look like this:

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

Breakdown:

  1. Checkout Repository checks out the Repository code to the Action
  2. Install Flutter gets the Flutter code from the Flutter Repository on Github
  3. Adds Flutter to the Path of the Github Action, so it can use the flutter cli
  4. Installs the dependencies of the app
  5. Runs flutter analyze to analyze the code and check for mistakes like unused imports
  6. 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
  7. 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

When someone makes a code request with good code quality:

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.

All code in this repository will be available in template form here: https://github.com/theswerd/flutter-githubactions-testing-template

--

--

No responses yet