Unit testing in Flutter | Get Started

 

Flutter Unit Testing:

Unit testing plays a key role in software development might be in terms of website, mobile app and what ever the medium may be testing is crucial.

Unit-testing has its own importance as it is done by software developer will come to know the bugs in early stage and can rectify them much easily.

This will also improve his coding standards and improves developer experience as well.Also testing the complete app will be so easy and fast.

Now let’s us get started with flutter unit testing and it’s implementation.

 

Flutter Unit Testing Video Tutorial :

Go through the below playlist for more details in unit testing.

 

pubspec.yaml :

Add flutter test dependency to pubspec file.

dev_dependencies:
  flutter_test:

 

main.dart :

Let us add few function for which we will create flutter unit test cases.

class Main{

  int value = 0;

  void increment() => value++;

  void decrement() => value--;

  void incrementPlus2() => value+=2;
}

 

widget_test.dart :

Now under test folder in flutter project structure add test cases to widget_test.dart as shown below.

test('Value needs to be incremented',() {
  main.increment();
  expect(main.value, 1);
});

 

Firstly specify the test method in which we will specify the description of the test case.

test('Value needs to be incremented'

 

and then specify the empty function there after specify the implementation of test case.

() { });

 

Declare main object as below to make use of the functions specified in main.dart file.

final main = Main();

 

try to call increment method which we have specified in main.dart file.

main.increment();

 

then specify what you expect once the test case is run

expect(main.value, 1);

 

We expect main.value which is a variable declared in main.dart file to become 1.

 

Now we will see complete test case.

test('Value needs to be incremented',() {
  final main = Main();
  main.increment();
  expect(main.value, 1);
});

 

In the similar way try to create test case for decrement function make sure you implement correct logic for so.

test('Value needs to be decremented', () {
  final main = Main();
  main.decrement();
  expect(main.value, -1);
});

 

Grouping-Up of test cases :

We can group up the unit test cases so that they can be easily called when required by specifying the group as below.

group("Main Screen",(){

});

 

Similar to how we specify the test case we add group with a name and implementations.

 

Declaring global variable :

Now to avoid code re-use we specify setup block so that a global level variable is declared and used through out.

late Main main;

setUp((){
  main = Main();
});

 

Complete Code :

widget_test.dart:

import 'package:flutter_basics/main.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {

  late Main main;

  setUp((){
    main = Main();
  });

  group("Main Screen",(){

    test('Value needs to be incremented',() {
      main.increment();
      expect(main.value, 1);
    });

    test('Value needs to be decremented', () {
      final main = Main();
      main.decrement();
      expect(main.value, -1);
    });

  });


  test('Value needs to be incremented by 2',(){

    main.incrementPlus2();
    expect(main.value, 2);
  });
}

 

unit testing output :

The screen below depicts the usage of flutter unit testing implementation

unit testing

 

If you have any query’s in the implementation of flutter unit testing do let us know in the comment section below. If you like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
useMemoized – Flutter hooks | A brief overview

  useMemoized : useMemoized, In this blog we will be dealing with the usage of caching concept in flutter apps...

Close