Unit test: What is it and how to use it in Flutter

So you’re a Flutter developer who wants to improve your skills and write better apps. One way to be a better developer is to apply Unit Testing to your Flutter apps. Unit tests are handy for verifying the behavior of a single function, method, or class. The test package provides the core framework for writing a unit test, and the flutter_test package provides additional utilities for testing Widgets.

Unit test in flutter
Unit test in Flutter

Writing unit tests for your Flutter app can help you create a resilient and bug-free set of code. So now, we – ArrowHiTech would like to introduce to you guys the definition of unit test, as well as how to use it in Flutter. To be more specific, in this tutorial, you’ll learn how to write this for your flutter app by building and testing a simple app for your business. Let’s get into the creation, shall we?

What is the act of a unit test?

Unit test is a process where you check quality, performance, or reliability by writing extra testing code that ensures your app logic works the way you expect before putting it into widespread use. Picture walking into your favorite apparel shop, taking a trendy shirt, and going into the fitting room. Writing unit tests helps you to confidently build new features, minimize bugs, and painlessly refactor existing code.

How to use unit test in Flutter?

Before you can start writing a unit test, you need to structure your code in such a way that you can easily test a single component of your app. One rule of thumb when building a testable architecture is that you should never import any UI-related packages into your suite. If you are importing UI-related packages, it means your unit tests are testing Flutter widgets instead of app logic, which isn’t what you want.

With these concepts equipped, you’re ready to dive into the starter project to see it in action!

1. Add the test dependency

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

 dev_dependencies:
  test: <latest_version>

2. Create a test file

In this example, create two files: counter.dart and counter_test.dart. The counter.dart file contains a class that you want to unit test and resides in the lib folder. The counter_test.dart file contains the tests themselves and lives inside the test folder.

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, this is the convention used by the test runner when searching for tests. When you’re finished, the folder structure should look like this:

 counter_app/
  lib/
content_copy
    counter.dart
  test/
    counter_test.dart

3. 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.

 content_copy
class Counter {
  int value = 0;

  void increment() => value++;

  void decrement() => value--;
}

4. 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.

 content_copy
// Import the test package and Counter class
import 'package:test/test.dart';
import 'package:counter_app/counter.dart';

void main() {
  test('Counter value should be incremented', () {
    final counter = Counter();

    counter.increment();

    expect(counter.value, 1);
  });
}

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 unit test package.

 content_copy
import 'package:test/test.dart';
import 'package:counter_app/counter.dart';

void main() {
  group('Counter', () {
    test('value should start at 0', () {
      expect(Counter().value, 0);
    });

    test('value should be incremented', () {
      final counter = Counter();

      counter.increment();

      expect(counter.value, 1);
    });

    test('value should be decremented', () {
      final counter = Counter();

      counter.decrement();

      expect(counter.value, -1);
    });
  });
}

6. Run the unit test

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 the unit test because it provides the fastest 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 Run 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 unit test by executing the following command from the root of the project:

 content_copy

flutter test test/counter_test.dart

Final words

That should be it! Here’s the complete definition of a unit test, as well as how to use that in Flutter app development. After all, by reading this Unit Testing with Flutter tutorial, you’ll improve your programming skills by learning how to add a unit test to your apps. Also, your business using Flutter app development will also become much more profitable than ever before.

We – ArrowHiTech is proud to be one of the most well-established outsourcing companies all around the world. That is to say, with over 12 years of experience, we can provide the best Flutter app development services for your business. Also, if you want to go through our showcase, you should visit our product page here: MageSolution or our portfolio: ArrowHiTech portfolio.

ArrowHiTech services

If you have any questions, don’t hesitate to contact us via email support@arrowhitech.com or our phone number +84 243 7955 813. We would love to hear every feedback from you guys.

Tags

Share