Unit Test

Unit Test

Flutter has become a popular framework for developing cross-platform mobile applications. Along with its ease of use and fast development cycle, ensuring the reliability and quality of Flutter apps is crucial. One powerful tool in achieving this is unit testing, which allows developers to test individual units or components of their codebase independently. In this article, we will delve into Flutter unit testing using Flutter Test, a framework provided by Flutter for writing and executing tests.

What is Flutter Test? Flutter Test is a testing framework provided by Flutter for writing and executing unit tests for Flutter applications. It is based on the Dart programming language, which is used for Flutter app development. Flutter Test provides a comprehensive set of utilities and APIs for writing tests, organizing test suites, and asserting expected outcomes.

Before we start, it is necessary to add flutter_test: under dev_dependencies: in the pubspec.yaml file.

pubspec.yaml page

In general, test files should reside inside a?test?folder located at the root of your Flutter application or package. Test files should always end with?test.dart (Examble : countertest.dart , homepage_test.dart), this is the convention used by the test runner when searching for tests.


Create a class to test :

Next, you need a “unit” to test. Remember: “unit” is another name for a function, method, or class. For this example, create a?Counter?class inside the?lib/counter.dart?file. It is responsible for incrementing and decrementing a?value?starting at?0.

Counter Class

Write a test for our class :

Inside the?counter_test.dart?file, write the first unit test. Tests are defined using the top-level?test?function, and you can check if the results are correct by using the top-level?expect?function. Both of these functions come from the?test?package.

Example Test Page

  • Initially, every test must be written inside the main() function.
  • The function used to write the test is test('Comment Message', () {});, where there are two parameters. The first parameter is a message displayed in the console to distinguish each test from the other, and the second parameter is a function where the test code for this unit is written.
  • A single function can contain multiple tests to simulate different scenarios, such as initialization tests, test success, or test failure.
  • The codes inside the test function can be divided into three groups:

  1. Arrange: These are the codes needed before starting the test process, such as instantiating a builder from the class.
  2. Act: These are the operations that simulate the function's operation. We use the 'when' function to simulate this operation or call the functions inside the class.
  3. Assert: Here, we write the test codes for the test result, and we use the functions 'expect' and 'verify'.



Counter Test Function:

Init Test Function

In this example, we are testing the initial value of the page before performing any functions.

  • In the arrangement step, we define the constructor.
  • In the action step, we assign the count value to the variable 'value'.
  • In the assertion step, we expect the test result to be zero.


Note : The expect function has two parameters: the first one is the value we are testing, and the second one is the expected result. If they are equal, the test passes; otherwise, the test fails.


Increment Test Function


In this example, we are testing the incrementCounter function of the Counter class.

  • In the arrangement step, we create an instance of the Counter class.
  • In the action step, we invoke the incrementCounter function.
  • Then, we retrieve the value of the counter. Finally, in the assertion step, we expect the value of the counter to be incremented by one.


Decrement Test Function


In this test, we are evaluating the decrementCounter function of the Counter class.

  • During the arrangement phase, we initialize an instance of the Counter class.
  • Then, in the action phase, we execute the decrementCounter function.
  • Next, we retrieve the current value of the counter.
  • Finally, in the assertion phase, we expect the counter value to be decremented by one.


This is the introductory article on unit testing. In the second article, we will discuss unit tests for classes that fetch data from live web services or databases, and we will use the Mocktail package.


Mohammad Naif Almowel

Software Developer | Mobil Application Developer | Flutter Developer | Android Developer(Native)

1 年

Love this ????

要查看或添加评论,请登录

Alaa Awad的更多文章

社区洞察

其他会员也浏览了