Unit Testing in Flutter

Overview

Unit testing a type of software testing that tests functionality of isolated units of code in the system. Unit testing code increases the confidence of the code working in the entire system. However, it does not guarantee that the code works perfectly, and bugs can still exist in the system.

The Autonomy team unit tests code to catch and fix bugs early on in the development cycle and to ensure that a component in the system works as expected, so it is easier to integrate in the overall system.

Unit Tests in Flutter

Unit testing in Flutter can be done with the flutter_test framework. The instructions below explain how to create a test file, write tests for your code, and run the tests.

  1. Add the flutter_test dependency in the pubspec.yaml file.

  2. Create a test file. If the file you are unit testing is named counter.dart then the test file should be named counter_test.dart .

    1. Test files should be created in a test directory which is at the same level as the directory the code you are writing tests for is in.

    2. Here is an example of a class in Flutter.

class Counter { int value = 0; void increment() => value++; void decrement() => value--; }
  1. Create a main function in the test file and initialize the object you are trying to test. If you are testing a class named Counter, then you will create an object of the Counter class and use it for testing purposes.

    1. Here is an example of a test file in Flutter.

  2. Using the functions in the flutter_test framework, you can assert the expected value of the code given the provided input and ensure that all of the test cases pass.

    1. Here is an example of a test file in Flutter.

// Import the test package and Example class import 'package:counter_app/counter.dart'; import 'package:test/test.dart'; void main() { test('Counter value should be incremented', () { // Test description final counter = Counter(); // Initialize object counter.increment(5); // Increment counter value expect(counter.value, 1); // expect(Value_from_class, Expected_Result) }); }
  1. Multiple tests can be combined in a group to organize all of your test cases and make it more readable.

    1. Here is an example of a group of tests in Flutter.

import 'package:counter_app/counter.dart'; import 'package:test/test.dart'; void main() { group('Counter', () { //Group of tests test('value should start at 0', () { // first test expect(Counter().value, 0); // expected class value checking }); test('value should be incremented', () { //second test final counter = Counter(); //functionality test start counter.increment(); //functionality test end expect(counter.value, 1); // expected class value checking }); test('value should be decremented', () { // third test final counter = Counter(); //functionality test start counter.decrement(); //functionality test end expect(counter.value, -1); // expected class value checking }); }); }
  1. Run the tests. Ensure that all of your test cases pass. If a test case doesn't pass, look at your code and your test case and see if the code you wrote is incorrect, or your test case is not written correctly.

    1. You can run tests in the terminal with flutter test test/counter_test.dart

    2. The output should look like this 00:06 +2: All tests passed!

For more information about the test package, see the test package documentation.

References

https://docs.flutter.dev/cookbook/testing/unit/introduction

https://pub.dev/packages/test

https://codelabs.developers.google.com/codelabs/flutter-app-testing?hl=en#4

Â