X
    Categories: Flutter

Flutter Unit Testing Tutorial For Beginners

 

Flutter Unit Testing :

Flutter unit testing tutorial provides a way of testing your flutter apps, this tutorial is oriented for the beginners.

Testing is the important step in app development as it plays a crucial role in app functionality, proper testing needs to be done so as to enable hassle free usage of app by the user.

As the app grows testing becomes much more complex so we perform this unit testing such that every block of the code is tested properly and ensures the perfect output.

Dividing the code into small blocks for performing the testing as well as reviewing is faster and reliable way of testing the code.

As the release of flutter 2.0 it’s much easier for testing as null safety is introduced making the app much more reliable and secure.

Because now additional 4 platforms like web app, windows, mac & linux with the current android and iOS platforms so testing is crucial step.

 

 

pubspec.yaml :

Add dependency for unit testing in dependency test and here we are not specifying any version you can provide the latest available regarding this plugin.

dependencies:
  flutter:
    sdk: flutter
  test:

 

main.dart :

Let’s consider a small example using which we will explain the unit testing where we increment and decrement the value

import 'package:flutter/material.dart';

class Counter {
  int value = 0;

  void increment() => value++;

  void decrement() => value--;
}

 

counter_test.dart :

Here we will consider another class where we provide the proper test cases for both increment and decrement.We are adding “_test” for the file to make it easier to understand it as testing file.

 

Test case for incrementing

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

  counter.increment();

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

 

Test case for decrementing

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

  counter.decrement();

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

 

Here we are incrementing and decrementing the value by 1 and you can adjust the counter value accordingly.

 

Full Code :

Providing the full code for unit testing integration

// Import the test package and Counter class
import 'package:flutter_unit_testing/main.dart';
import 'package:test/test.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);
    });
  });
}

 

Run the test :

The flutter unit testing is performed in android studio using the below command in terminal, then a file is generated for testing scenarios incrementing and decrementing. For more info may refer

flutter test test/counter_test.dart

 

 

abhishek:
Related Post

This website uses cookies.