Table of contents
1.
Introduction
2.
Unit Testing in Dart
2.1.
Setting Up Project
2.2.
Writing Sample Function
2.2.1.
Code:
3.
Writing Unit Test
3.1.
Output:
4.
Handling The Failing Tests
4.1.
Code:
4.2.
Output:
5.
Frequently Asked Questions
5.1.
In Flutter, what kinds of tests can you run?
5.2.
What are the advantages of API testing?
5.3.
What is the future scope of Dart?
6.
Conclusion 
Last Updated: Mar 27, 2024
Medium

Dart Unit Testing

Author Akshit Pant
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Testing each individual unit of an application is known as unit testing. It allows the developer to test small features without having to run the complete application.

Let us assume that you have a basic understanding of programming languages and are familiar with basic Dart Introduction. In this blog, we will be covering Dart Unit Testing and its implementation in our program.

So, let's start with the basics of Dart Unit Testing.

Unit Testing in Dart

Unit testing is the process of individually evaluating each component of a software/application to ensure that it functions as intended.

It has numerous advantages, including:

  • Individual components can be tested without having to run the entire software/application.
  • It's simple to pinpoint faults within a single component.

Before we deep dive into Dart Unit Testing, know that you can develop your Dart sample code on DartPad.

Dart has a package named test that makes unit testing relatively simple for developers. We'll construct a few unit tests with this package in this article, showcasing various use cases and outcomes.

Setting Up Project

Because we're using the test, which is an external dart package, we'll need to establish a new project to run the project.

Create a pubspec.yaml file and include the lines below in the project.

name: PerformDartTesting
dev_dependencies:
  test:

Then we must run the command below.

dart pub get

This will create a PerformDartTesting project and install the test package as a development dependency. Then we can import the test package into a main.dart file that will include all of our code.

import 'package:test/test.dart';

Writing Sample Function

First, we must create a simple function that will be used to execute our tests. The code for calculating the sum of all the items of an array is provided below.

Code:

int getSumInArray(List<int> arr) {   // to get the sum of all elements in array
  int ans = 0;
  for (var j=0; j<arr.length; j++) {
    ans += arr[j];
  }
  return ans;
}

Writing Unit Test

The test() function can be used to write a unit test for the above code. It incorporates two arguments.

  • A string value holding a unit test description.
  • The expect() function is used to create a test assertion. And, Two values must be provided to the expect() function: Expected Value and Actual Value.

The developer must manually give the Expected Value. And, Actual Value, is determined by performing the relevant function. If these values match, the Unit Test is regarded successful; otherwise, it is considered unsuccessful.


The code for a unit test for the getSumInArray() function is provided below. 

Code:

main() {
    test('Getting sum in array : onSuccess', () {
      int expectedValue = 35;
      int actualValue = getSumOfArray([5, 5, 8, 8, 9]);
      expect(expectedValue, actualValue);
    });
  }

It will be a successful test because the expected and actual numbers match.

Output:

00:00 +0: Getting sum in array : onSuccess
00:00 +1: All tests passed!

Handling The Failing Tests

The expected value differs from the actual value in this situation, causing the test to fail.

Code:

main() {
    test('Getting sum in array : onSuccess', () {
      int expectedValue = 50;
      int actualValue = getSumOfArray([5, 5, 10, 10, 15]);
      expect(expectedValue, actualValue);
    });
  }

Output:

00:00 +0: Getting sum in array - failure

00:00 +0 -1: Getting sum in array - failure [E]

  Expected: <50>
    Actual: <45>

Frequently Asked Questions

In Flutter, what kinds of tests can you run?

Unit tests: A unit test examines only one function, method, or class.
Widget tests: A widget test (also known as a component test in other UI frameworks) examines a single widget.
Integration tests: An integration test examines the entire app or a significant portion of it.

 

What are the advantages of API testing?

API testing is a type of software testing in which an application programme interface (API) is examined to ensure that it meets functional, security, performance, and reliability requirements. The tests are carried out either on the API or as part of integration testing.


What is the future scope of Dart?

Dart, without a doubt, has a bright future ahead of it. Dart's development team has spent the last year working to make it the best language for client-side web programming with the AngularDart framework and cross-platform development with the Flutter framework. It has outperformed react native on both GitHub and Stack Overflow.

Conclusion 

In this article, we learned about Dart Unit Testing and its working implementation by showcasing a working example.

If you are a beginner in the dart, here are some interesting articles you want to read, Sets, Map, Symbol, Operator, Recursion, Variables, Libraries and Inheritance in Dart.

Happy Coding!

Live masterclass