Testing
Learn to test the app logic and UI when working with GetX.
We'll cover the following
Overview
Testing is an important aspect of any app development, whether it’s a minimal prototype or a full-blown production app. It reduces the chances of bugs when adding new features and speeds up the development over the long term. Flutter enables three kinds of tests: unit tests to test pure logic, widget tests to test individual widget behavior, and integration tests to test the entire application.
In this lesson, we will learn how to run unit tests on GetX classes, specifically GetxController
to verify its life cycle controls and test individual methods independently. We’ll also write a widget test to verify that our app’s state changes work as expected. As an example, we will build a simple counter app that displays the count and gives us the ability to increment it. We will test the life cycle of the controller and business logic in the unit tests. And, we will run a widget test to see if the counter increments visually. So, let’s get started!
Building the app
Before we write the tests, let us build the counter app first. Follow the steps below:
Start by creating a controller called
CounterController
. It’s a minimal class with acounter
variable and functions to increment and decrement it. We’ve also writtenonInit
andonClose
methods because we want to test the controller life cycle in the unit tests.
Get hands-on with 1400+ tech skills courses.