Book Image

Dart By Example

By : David Mitchell
Book Image

Dart By Example

By: David Mitchell

Overview of this book

Table of Contents (17 chapters)
Dart By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Unit testing


If you are a Test Driven Development advocate you are probably a bit concerned that we have not written any unit tests so far. That is about to change as we introduce the Dart unit test package, which is simply called test (you may see older example code online that uses the older unittest package).

In the sample code for this chapter, there is a sub-folder called Unittest that contains a unit test in bin/main.dart:

library Unittestdemo.test;

import 'package:test/test.dart';

void main() {

  test('HelloWorldTest', () {
    expect(1+1, 2);
  });

}

This defines a new test called HelloWorldTest and the actual test can be carried out by the past in function. The result of the test can be validated using the expect method. The library contains an extensive range of matchers to check a result, for example, isResult, isList, isNull, isTrue and isNonPositive.

Note

Unit Testing focuses on testing a small part of an application at a time. Tests are defined in code, so that they can be run...