Unit Testing and Test-Driven Development in Dart

  • Unit Testing and Test-Driven Development in Dart

Unit Testing and Test-Driven Development in Dart

By the name we can guess what type of testing is Unit Testing. Unit Testing involve testing every individual unit of an application. It helps developer to test small functionalities without running/testing thw whole application. Dart unit testing involves the following steps -

  • Step 1: Installing the “test” package

To installing third-party packages in the current project, you will require the pubspec.yaml file. To install test packages, first make the following entry in the pubspec.yaml file -

dependencies: 
test:

After making the entry, right-click the pubspec.yaml file and get dependencies. It will install the “test” package. Given below is a screenshot for the same in the WebStorm Editor. Packages can be installed from the command line too. Type the following in the terminal − pub get

  • Step 2: Importing the “test” package
import "package:test/test.dart";
  • Step 3 Writing Tests

Tests are specified using the top-level function test(), while test assertions are made using the expect() function. For using these methods, they should be installed as a pub dependency.

Syntax

test("Description of the test ", () {  
   expect(actualValue , matchingValue) 
});

The group() function can be used to group tests. Each group’s description is added to the beginning of its test’s descriptions.

Syntax

group("some_Group_Name", () { 
   test("test_name_1", () { 
      expect(actual, equals(exptected)); 
   });  
   test("test_name_2", () { 
      expect(actual, equals(expected)); 
   }); 
})