Flutter Automated Tests — Getting Started

A look at automated unit and widget tests

Albert Oboh
Oct 28 · 5 min read

Automated tests in software development help you catch bugs and different sorts of issues with your app before you push it. This is especially useful for large apps where testing every part manually might not be possible.

Lucky for us, Flutter has different types of tests we can do.

We will look at unit and widget tests in this piece.

Unit Tests

Unit testing is breaking your code into simple parts (units), and these parts are then tested. These units are usually functions, methods, classes, or even variables. Let’s try it out!

First, you should have the flutter_test package installed in your dev dependencies in your file.

If you’re building a Dart app that does not depend on Flutter, you can install the test package as a dev dependency.

flutter_test dependency.

Next, create a class with the code you’d like to test, I’ve named mine , and also create the test file for this class, I’ve named mine .
The file where your code is can be in the folder and your tests should be in the folder on the root.

The code in the class we want to test is as follows:

The code we want to test we.dart

Here, we have a simple class with one method and one string instance. The string, should be edited with the sum of the parameters passed to the method after it is called.

Our test file looks like this:

Our test file we_test.dart

First, we import the flutter_test package and the class we want to test. Then, we start writing.

We start with the method from the flutter_test package. The method accepts two parameters. First, the description of the test and second, a function with the tests to run.

Here, we first set the class , which we imported to a variable, then we call the method, passing in params two and three. Therefore, we expect the result of the sum to be five.

We then call the method from flutter_test, which is used to assert that the unit we are testing matches what is returned when the code was run. These two are called the actual and the matcher respectively.

If they match, then our tests pass, if they don’t, then it fails.

Run the test

You can run the test by opening the test file () and then run debug from the debugger on VS Code.

Or, run in your terminal.

Widget Testing

Widget tests do exactly what they imply: test widgets.

Widget tests are different from unit tests in that they require classes and functions different from those in unit tests. These tools are part of the package.

Let’s get to it!

At this point, you should have the package installed through your file.

Next, we need widgets to test, so we’ll create a screen with some simple widgets:

The screen with our widgets.

The output is a screen with a text field (and a controller), a and a text widget (for showing the inputted text).

What it looks like.

Now that we have widgets, let’s write our tests:

Widget tests.

Immediately, you should start spotting the differences between this and the unit tests we wrote previously.

  1. The function, which creates a new for each test, is used in place of function in unit tests.
  2. The , which is used to locate individual widgets.
  3. The Matcher (, , ), which we use to verify the widgets.
  4. , which Flutter uses to build a widget. You can also set duration for things like animations.

What’s going on here?

  • First, we’re using the function and passing a description for this test, and the to be used with every test case.
  • Then, we build the widgets by calling .
  • Our first use of the class is to find the raised button on the screen. We use the method here, but there are a lot more we can try, which I’ll try to show by using later on, to find the text field with the key assigned to it.
  • Next, we try to test the . First, we verify that there is a with the finder and matcher, and then trigger a rebuild of the views.
  • Calling the method from the allows Flutter to input text into the found, then rebuild views again.
  • Finally, we ask Flutter to check that the text printed on the screen is the same as the text that was inputted in the previous step.

Run the tests via your editor or run in your terminal. This test should pass, but play with it and see the different ways things can break.

Also, check out all the methods and examples in the classes used.

In a continuation of this, I might write about mocking internet calls and integration tests.

Better Programming

Advice for programmers.

Albert Oboh

Written by

Software developer, flutter lover.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade