How can you ensure that your app continues to work as you add more features orchange existing functionality? By writing tests.

Unit tests are handy for verifying the behavior of a single function,method, or class. The test package provides thecore framework for writing unit tests, and theflutter_testpackage provides additional utilities for testing Widgets.

This recipe demonstrates the core features provided by the test package.For more information about the test package, see thetest packagedocumentation.

Directions

  • Add the test or flutter_test dependency
  • Create a test file
  • Create a class to test
  • Write a test for our class
  • Combine multiple tests in a group
  • Run the tests

1. Add the test dependency

If you’re working on a Dart package that does not depend on Flutter, youcan import the test package. The test package provides the core functionalityfor writing tests in Dart. This is the best approach when writing packages thatwill be consumed by web, server, and Flutter apps.

  1. dev_dependencies:
  2. test: <latest_version>

2. Create a test file

In this example, create two files: counter.dart and counter_test.dart.

The counter.dart file will contain a class that you want to test andresides in the lib folder. The counter_test.dart file will containthe tests themselves and lives inside the test folder.

In general, test files should reside inside a test folder located at the rootof your Flutter application or package.

When you’re finished, the folder structure should look like this:

  1. counter_app/
  2. lib/
  3. counter.dart
  4. test/
  5. counter_test.dart

3. Create a class to test

Next, you need a “unit” to test. Remember: “unit” is a fancy name for afunction, method, or class. In this example, create a Counter classinside the lib/counter.dart file. It will be responsible for incrementingand decrementing a value starting at 0.

  1. class Counter {
  2. int value = 0;
  3. void increment() => value++;
  4. void decrement() => value--;
  5. }

Note: For simplicity, this tutorial does not follow the “Test DrivenDevelopment” approach. If you’re more comfortable with that style ofdevelopment, you can always go that route.

4. Write a test for our class

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

  1. // Import the test package and Counter class
  2. import 'package:test/test.dart';
  3. import 'package:counter_app/counter.dart';
  4. void main() {
  5. test('Counter value should be incremented', () {
  6. final counter = Counter();
  7. counter.increment();
  8. expect(counter.value, 1);
  9. });
  10. }

5. Combine multiple tests in a group

If you have several tests that are related to one another, combine them using the group function provided by the test package.

  1. import 'package:test/test.dart';
  2. import 'package:counter_app/counter.dart';
  3. void main() {
  4. group('Counter', () {
  5. test('value should start at 0', () {
  6. expect(Counter().value, 0);
  7. });
  8. test('value should be incremented', () {
  9. final counter = Counter();
  10. counter.increment();
  11. expect(counter.value, 1);
  12. });
  13. test('value should be decremented', () {
  14. final counter = Counter();
  15. counter.decrement();
  16. expect(counter.value, -1);
  17. });
  18. });
  19. }

6. Run the tests

Now that you have a Counter class with tests in place, you can run the tests.

Run tests using IntelliJ or VSCode

The Flutter plugins for IntelliJ and VSCode support running tests.This is often the best option while writing tests because it provides thefastest feedback loop as well as the ability to set breakpoints.

  • IntelliJ
    • Open the counter_test.dart file
    • Select the Run menu
    • Click the Run 'tests in counter_test.dart' option
    • Alternatively, use the appropriate keyboard shortcut for your platform.
  • VSCode
    • Open the counter_test.dart file
    • Select the Debug menu
    • Click the Start Debugging option
    • Alternatively, use the appropriate keyboard shortcut for your platform.

Run tests in a terminal

You can also use a terminal to run the tests by executing the followingcommand from the root of the project:

  1. flutter test test/counter_test.dart