Testing in Flutter

Sep 19 2023 · Dart 2.19.3, Flutter 3.7.6, Android Studio 2021.3.1, Visual Studio Code 1.7.4

Part 2: Write Unit Tests

04. Your First Unit Test

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. What Are You Going to Test? Next episode: 05. Test Change Notifier

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 04. Your First Unit Test

Now that you know what you will test, let us start with writing our first unit test. Head over to the test folder and create a new file called quotes_api_test. You will be writing your first unit test in this file, and as the name suggests, you will be testing the quotes API. But before we start testing the API, let us first write a simple test to see how the test works.

As you know, the flutter engine requires a main function to run the app. Similarly, the test engine requires a main function to run the tests. So let us write a main function.

void main() {

}

The test engine will call this main function to run the test, and all our test code will be inside this main function. Looks familiar, right?

You will call a function named test, which takes a string and a function as an argument. The string is the name of the test, and the function is the test function. To call this function, you need to import the test package. Write the following code on top to import the test package.

import 'package:flutter_test/flutter_test.dart';

Now you are good to use the test function. Let’s write a simple Unit test.

void main() {
  test('Simple Test', () {
    expect(1, 1);
  });
}

Good job! You have written your first unit test. Now, let us understand what is happening here. We have named our test as Simple Test, which is a simple function that checks if 1 equals 1. If it is true, then the test passes, else it will fail.

The expect function takes two arguments. The first argument is the actual value and the second argument is the expected value. If the actual value is equal to the expected value then the test passes else it fails.

Now let us run the test. To run the test you can use the following command.

flutter test test/quotes_api_test.dart

or you can run the test from the IDE by clicking the play button on the left side of the test function, or by clicking on the Run|Debug on top of the test function.

You can see the test passes. Now let us change the expected value to 2 and see what happens.

void main() {
  test('Simple Test', () {
    expect(1, 2);
  });
}

Run the test, and now you can see the test fails.

Now let’s test the Quotes Model. We have a Quotes Model class in the model folder. Let us write a test to check if the Quotes Model is working fine.

Replace the Simple Test with the following code.

test('quotes json test', () {
  final quote = Quotes(1, '', '');

  expect(quote.id, 1);
  expect(quote.author, '');
  expect(quote.quote, '');
});

Here we have created a new instance of the Quotes Model and are checking if the id, author and quote attributes are equal to the expected value.

Bravo! You have written your first unit test. This is how you write a unit test. You can write as many unit tests as you want. In general, you should write a unit test for every scenario.

Testing API is tricky. You need to mock the API response and then test the API. We will come back to test API once we have covered the Mocking of Data.