Dart Unit Testing

In this tutorial you will learn about the Dart Unit Testing and its application with practical example.

Unit Testing

Unit Testing is a software development process where individual units/components of an application are tested. The purpose is to validate each of the individual units of an application performs as designed. Basically we can think of a unit as smallest testable parts of an application that can be logically isolated in a system. In most programming languages it can an individual program, a function, a subroutine, a method, property or a class.

Advantages of Unit Testing

1.) Unit testing makes it easy to maintain code.

2.) It makes code are more reusable.

3.) It helps to faster the development

4.) It makes code more reliable

5.) It makes debugging is easy

6.) It reduces cost of testing and fixing of defects, as they are captured in very early stage.

Dart Unit Testing

In Dart, we need to incorporate external library named “test”, which provides a standard way of writing and running individual unit tests. In Dart, unit testing can be done in following steps –

Installing “test” package :- To incorporate unit testing in your project, you need to install a third-party packages “test” in the your project. Lets open pubspec.yaml in your project and make the following entry in it.

Now, right-click on pubspec.yaml file and click get dependencies. This will install the “test” package in your project.


It can also be installed from command line using following command –

Importing “test” package :- Add the following line of in your test file.

Writing Test Cases :- Test cases are added using a top-level function test(), while test assertions are made using the expect() function.


Grouping Of Test Cases

Multiple test cases can be grouped together using the group() function. It helps grouping of test cases based on some criteria.


Example (Passing Test) :-

The following example we have defined a method Add(), which takes two integer values and returns an integer representing the sum of the number. Lets run some test on add() method –

When we run the above dart program, we will see following output.



In this tutorial we have learn about the Dart Unit Testing and its application with practical example. I hope you will like this tutorial.