What is GitHub Actions?
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.
You can find more info about it here.
Why integration tests are important?
Unit tests and Widget tests are handy for testing individual classes, functions, or widgets. However, they generally donβt test how individual pieces work together as a whole, or capture the performance of an application running on a real device. These tasks are performed with integration tests.
Integration tests are written using the integration_test package, provided by the Flutter SDK.
How to write integration tests?
Assuming you have already created a Flutter project with the integration_test
package added as a dependency. You can take a look at the integration testing documentation to learn how to write integration tests.
For example, a simple integration test would look like this:
Workflow Overview
Here is a quick overview of the steps involved in the workflow we are going to create:
Setting up GitHub actions
First of all, you need to create a workflows
folder inside the .github
folder.
Then, you need to create a testing.yml
file inside the workflows
folder. This file contains the configuration for our integration tests.
Your folder structure should look like this:
Now, itβs time to setup the basic workflow:
This job is triggered when you push new changes on the default branch. The first step that we will use is to checkout the code of our branch at the particular commit.
Setting up Flutter and Android
First thing we need to do is to setup Java in our workflow, we can easily do it using setup-java provided by GitHub.
We will be using Java 11
with Zulu as distribution channel:
Now, we need to setup Flutter in our workflow. We can easily do it by using another action provided by subosito with stable
channel as the default:
Also, we need setup an emulator for our tests. We can setup the sdk using malinsky/action-android:
ndk
is not necessary for our tests, it is just shown as an example of what I use in production. You can install specific version of components you require just like that.
Running the tests
We need to first get all the packages that we need to run our tests. We can do it by using the command in the workflow:
Finally, we can run our tests while the emulator is running using the following action:
cmd
is the command for running the tests and api
is the API level of the emulator.
Note
We can also use KVM now to speed up the emulator.
TIL the new Github Actions ubuntu-latest images support KVM and can run Android emulator tests faster than macOS-latest. They're free for public repos. Pretty awesome! pic.twitter.com/1ZNIIxQmhx
β Colin White (@colinwhi) January 22, 2024
Final Workflow
The final testing.yml
workflow should look something like this:
And here is the workflow in action:
Conclusion
Testing can be a bit of a challenge, but itβs worth it. Once you start writing some tests you can start to see how you can improve your code and app workflow.
I hope this helped you to get started with testing in Flutter and how to use GitHub Actions to automate your tests. Share it with others if you found it helpful.