How to create Flutter tests using Github Actions
Github actions
Github Actions automate your workflow to maintain the app integrity and avoid any code breakage during production.
Creating workflows
-
To add workflow to your existing Flutter repository, simply choose
Actions. -
Create a new workflow: You’ll see many pre-designed workflows for various services, right from AWS to Kubernetes, depending on the type of repository you are on.
-
Click on
setup a workflow yourself, and it will create a predefined.ymlfile. You can even rename your workflow if you’d like. -
Delete the contents of the
.ymlfile and paste the following code below:
name: Flutter Test
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '11'
- uses: subosito/flutter-action@v1
with:
flutter-version: '2.0.5'
- name: Get all Flutter Packages
run: flutter pub get
- name: Run Flutter Test
run: flutter test
-
In the script above, the
namewould be your workflow name. Theoncommand handles when the workflow shall be triggered on pushing the code, andpull_requestwill trigger the workflow when a pull request is triggered. -
In
branches, you can specify which branch of the code you would want to reflect the push or pull changes and run the workflow. -
The
jobsis where you would define the workflow jobs that would execute it. In our instance, it would run on an Ubuntu machine. The same can be replaced with a Mac, Windows, or even any other Linux OS. -
The
stepsis the set of steps that it needs in order to execute/install the needed pre-requisites. Here, we are installing Java and a Flutter action that was previously created for GitHub Actions. -
The
runwill execute the workflow commands. Since we are merely just running the test command to test our Flutter project, we shall just use theflutter test. -
Now, whenever you push or create a pull request from the master branch, the Github workflow will be triggered and would be automatically checking for tests.