How to Run Flutter Tests on GitHub Actions

Pradap Pandiyan
3 min readJan 26, 2025

--

Flutter is a powerful framework for building cross-platform mobile, web, and desktop applications. Automating tests for Flutter projects is essential to ensure code quality and streamline development workflows. GitHub Actions, with its native CI/CD capabilities, provides an excellent platform for running Flutter tests automatically. In this article, we’ll guide you through setting up a GitHub Actions workflow to run Flutter tests for your project.

Prerequisites

Before setting up the GitHub Actions workflow, ensure the following:

  • You have a Flutter project hosted on GitHub.
  • Your project includes tests in the test directory.
  • Your pubspec.yaml file specifies compatible Dart and Flutter SDK versions.

Create a GitHub Actions Workflow File

GitHub Actions workflows are defined in YAML files located in the .github/workflows/ directory of your repository. Create a file named flutter-tests.yml:

name: Flutter Tests

on:
push:
branches:
- main
pull_request:

jobs:
flutter-tests:
name: Run Flutter Tests
runs-on: ubuntu-latest

env:
FLUTTER_CHANNEL: master
FLUTTER_VERSION: 3.28.0-0.1.pre

steps:
# Step 1: Checkout repository
- name: Checkout repository
uses: actions/checkout@v3

# Step 2: Install dependencies (apt packages)
- name: Install required libraries
run: sudo apt-get update -q -y && sudo apt-get install -y lib32stdc++6

# Step 3: Set up Flutter
- name: Clone Flutter SDK
run: |
git clone https://github.com/flutter/flutter.git -b $FLUTTER_CHANNEL
echo "$(pwd)/flutter/bin" >> $GITHUB_PATH

- name: Verify Flutter installation
run: |
flutter --version
flutter doctor

# Step 4: Install Flutter dependencies
- name: Install Flutter dependencies
run: flutter pub get

# Step 5: Run tests with coverage
- name: Run Flutter tests
run: flutter test --coverage

# Step 6: Upload coverage as artifact
- name: Upload coverage artifact
uses: actions/upload-artifact@v3
with:
name: coverage
path: coverage/

# Step 7: Parse and Display Coverage (Optional)
- name: Display test coverage
run: |
if grep -q 'All files.*:.*[0-9]\+\.[0-9]\+%' coverage/lcov.info; then
grep 'All files.*:.*[0-9]\+\.[0-9]\+%' coverage/lcov.info
else
echo "No coverage summary found."
fi

Explanation of Workflow Steps

  1. Checkout Repository:
  • The actions/checkout action is used to clone your repository into the workflow environment.

Install Dependencies:

  • Some dependencies, like lib32stdc++6, may be required for running Flutter commands.

Set Up Flutter:

  • The workflow clones the Flutter SDK from the specified channel (e.g., stable) and adds it to the system path.

Verify Installation:

  • Commands like flutter --version and flutter doctor ensure that Flutter is installed and configured correctly.

Install Flutter Dependencies:

  • The flutter pub get command fetches all the dependencies specified in your pubspec.yaml.

Run Tests:

  • The flutter test --coverage command runs your tests and generates a coverage report.

Upload Coverage Artifact:

  • Test coverage data is uploaded as an artifact for later inspection.

Display Test Coverage (Optional):

  • This step extracts the coverage percentage from the generated report and logs it in the workflow output.

Commit and Push

Commit the flutter-tests.yml file to the .github/workflows/ directory and push it to your repository:

git add .github/workflows/flutter-tests.yml
git commit -m "Add GitHub Actions workflow for Flutter tests"
git push origin main

Verify the Workflow

After pushing, GitHub Actions will automatically trigger the workflow on the specified events (push or pull_request). Navigate to the Actions tab in your repository to monitor the workflow's progress and review the results.

Validating the test Runner on the Github actions

When the changes are made the pipelines will be triggered.

Troubleshooting Common Issues

Flutter Version Mismatch:

  • Ensure the Flutter version in the workflow matches the version required by your project.

Dependency Errors:

  • Verify that all dependencies are specified correctly in pubspec.yaml.

Environment Path Issues:

  • Ensure Flutter is added to the PATH correctly using $GITHUB_PATH.

Conclusion

Setting up a GitHub Actions workflow to run Flutter tests is a straightforward process that enhances your CI/CD pipeline. By automating tests, you can identify issues early, improve code quality, and streamline collaboration. With this setup, your Flutter project will benefit from reliable and repeatable testing every time changes are pushed or pull requests are created.

I have created a project on Github and added the code here.

Feel free to hit clap if you like the content. Happy Automation Testing :) Cheers. 👏

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Pradap Pandiyan
Pradap Pandiyan

Written by Pradap Pandiyan

I’m a passionate QA Engineer. I’m a motovlogger, content creator, off-roader and freelancer. Buy me a coffee here https://www.buymeacoffee.com/pradappandiyan

Responses (1)

Write a response