Comprehensive Guide to Writing and Running Dart Tests with 'package:test'

Published on by Flutter News Hub

Comprehensive Guide to Writing and Running Dart Tests with 'package:test'

'package:test' is a standard framework for writing and running tests in Dart. This guide will provide a comprehensive introduction to its features, including:

  • Writing tests
  • Running tests with advanced options
  • Asynchronous tests
  • Custom HTML for tests
  • Configuring tests
  • Tagging and filtering tests
  • Debugging tests
  • Support for hybrid browser/VM tests

Writing Tests

Tests in Dart are written using the 'test()' function from 'package:test'. Assertions are made using 'expect' from 'package:matcher'.

import 'package:test/test.dart';

void main() {
  test('String.split() splits the string on the delimiter', () {
    var string = 'foo,bar,baz';
    expect(string.split(','), equals(['foo', 'bar', 'baz']));
  });
}

Running Tests

Tests can be run using 'dart test' or 'pub run test'. The latter is preferred in Dart SDK versions prior to 2.10.

You can run tests within a directory or for a specific test file. By default, tests are run on the Dart VM, but you can also run them in the browser using '-p chrome'.

Sharding and concurrency options are available to parallelize test execution.

Asynchronous Tests

Asynchronous tests are supported by 'package:test' and will not be considered complete until the returned 'Future' completes.

import 'dart:async';
import 'package:test/test.dart';

void main() {
  test('Future.value() returns the value', () async {
    var value = await Future.value(10);
    expect(value, equals(10));
  });
}

Configuring Tests

Tests can be configured using platform-specific annotations, timeouts, tags, and a package configuration file ('dart_test.yaml').

Tagging and Filtering Tests

Tags are used to categorize tests. You can filter tests based on tags using command-line flags ('--tags' to include, '--exclude-tags' to exclude).

@Tags(['browser'])
import 'package:test/test.dart';

void main() {
  test('launches two browsers at once', () {
    // ...
  }, tags: ['chrome', 'firefox']);
}

Debugging Tests

Tests can be debugged using built-in development tools. Use '--pause-after-load' to pause execution after each test suite loads. For VM tests, the remote debugger URL will be printed.

Hybrid Browser/VM Tests

Hybrid tests allow code to run on both the browser and the VM. Use 'spawnHybridCode()' or 'spawnHybridUri()' to create an isolate and communicate with it using a 'StreamChannel'.

import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';

void main() {
  @TestOn('browser')
  test('connects to a server-side WebSocket', () async {
    var channel = spawnHybridUri('web_socket_server.dart');
    var port = await channel.stream.first;
    var socket = WebSocket('ws://localhost:$port');
    var message = await socket.onMessage.first;
    expect(message.data, equals('hello!'));
  });
}

Support for Other Packages

'package:test' supports integration with other packages, such as 'build_runner' and 'term_glyph'.

Flutter News Hub