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.
Add the
flutter_test
dependency in thepubspec.yaml
file.Create a test file. If the file you are unit testing is named
counter.dart
then the test file should be namedcounter_test.dart
.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.Here is an example of a class in Flutter.
class Counter {
int value = 0;
void increment() => value++;
void decrement() => value--;
}
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.Here is an example of a test file in Flutter.
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.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)
});
}
Multiple tests can be combined in a
group
to organize all of your test cases and make it more readable.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
});
});
}
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.
You can run tests in the terminal with
flutter test test/counter_test.dart
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://codelabs.developers.google.com/codelabs/flutter-app-testing?hl=en#4
Â