How do we test Flutter applications? — Unit Test

Sanket Masurkar
Flutter Community
Published in
4 min readApr 20, 2022

Hello Everyone!👋

Let us first understand why we need to perform automated testing on our application while it’s still in development.

Automated testing is a method of writing and running test case suites on a software application using an automation tool testing tool.

We can save time by using automated tests to detect new bugs that may result in the emergence of routine changes if we test our application during the development phase.

Few types of Flutter Automated Testing:

  • Unit Test
  • Widget Test
  • Integration Test

Note: We will only discuss unit tests in this article.

What exactly are unit tests and test cases?

A Unit test is used to verify the correctness of a single function, method, or class. To test this function, method, or class, test cases must be written. The conditions that must be met in order to verify the expected output are referred to as Test Cases.

Implementation Procedures:

In this article, we will run the test cases for the Login page, which has two fields: email id and password.

Step 1: Ensure that flutter_test dependency exist in pubspec yaml file. If not then add it.

dev_dependencies:
flutter_test:
sdk: flutter

Step 2: Create Validator Class.

All of our validation methods will be written in this class. These methods will be used in our Login Page (inside email and password textformfield validator parameter) and login_unit_test classes.

  • Password TextFormField
TextFormField(
validator: Validators.validatePwd,
)
  • Validator Class

Step 3: Create login_unit_test class inside test directory and write test cases for Login.

So here we are, at the step where we will write our test cases.

void main() {

test('Verify invalid email address', () {
String emailId = 'abc@gmail';

var result = Validators.validateEmailId(emailId);
expect(result, 'Enter valid Email ID.');

});

test('Verify valid password', () {
String pwd = 'Qwe@12345';

var result = Validators.validatePwd(pwd);
expect(result, null);

});

}

We use the test function to define our test cases. There are two mandatory parameters that we must provide in this function. The first parameter is a string value, i.e. (the description for that specific case), and the second parameter is a function in which we validate our test case.

We use the expect function to compare our result to what we expected.
Expect function also has two required parameters. The first parameter will be the result which our validator function returns and the second parameter is the comparer or expected result.

The test package provides the test and expect functions.

Let’s return to the preceding example.

The first test function is to validate the email address. As you can see, there are two variables: emailId and result. After validating the emailId, our Validator class function returns the value to the result variable.

So, some of you may be wondering why I left the expected result value as null for the second test case.

If you proceeded to Step 3, where we created our validator class, you’ll notice that if the value is not empty or matches the regex, we return a null value. This is why the expected result value is set to null. To ensure that the result is correct.

You can also group your test cases together.

group('Multiple test cases for password verification', () {

test('Verify password', () {
String pwd = 'Abc@12345';

var result = Validators.validatePwd(pwd);
expect(result, null);

});

test('Verify empty password', () {
String pwd = '';

var result = Validators.validatePwd(pwd);
expect(result, 'Enter Password');

});

});

Step 4: Run test cases.

To run all your test cases you can simply type the following in your terminal:
flutter test test/name_of_your_file.dart

  • When all test cases pass, this is the result.
  • You can also run individual test cases by clicking the play button, which I have highlighted in the image below.
  • The following is the outcome if your test case fails.

Conclusion:

This was a quick overview of flutter’s unit testing.
I hope I’ve given you enough information to try unit testing in your project.

https://twitter.com/FlutterComm

--

--

Sanket Masurkar
Flutter Community

Flutter enthusiast by day, dart-throwing champion by night.