[![pub package](https://img.shields.io/pub/v/test.svg)](https://pub.dev/packages/test) [![package publisher](https://img.shields.io/pub/publisher/test.svg)](https://pub.dev/packages/test/publisher) `test` provides a standard way of writing and running tests in Dart. ## Using package:test * [Writing Tests](#writing-tests) * [Running Tests](#running-tests) * [Sharding Tests](#sharding-tests) * [Test Concurrency](#test-concurrency) * [Shuffling Tests](#shuffling-tests) * [Selecting a Test Reporter](#selecting-a-test-reporter) * [Collecting Code Coverage](#collecting-code-coverage) * [Restricting Tests to Certain Platforms](#restricting-tests-to-certain-platforms) * [Platform Selectors](#platform-selectors) * [Compiler Selectors](#compiler-selectors) * [Running Tests on Node.js](#running-tests-on-nodejs) * [Asynchronous Tests](#asynchronous-tests) * [Running Tests With Custom HTML](#running-tests-with-custom-html) * [Providing a custom HTML template](#providing-a-custom-html-template) * [Configuring Tests](#configuring-tests) * [Skipping Tests](#skipping-tests) * [Timeouts](#timeouts) * [Platform-Specific Configuration](#platform-specific-configuration) * [Whole-Package Configuration](#whole-package-configuration) * [Tagging Tests](#tagging-tests) * [Debugging](#debugging) * [Browser/VM Hybrid Tests](#browservm-hybrid-tests) * [Support for Other Packages](#support-for-other-packages) * [`build_runner`](#build_runner) * [`term_glyph`](#term_glyph) * [Further Reading](#further-reading) ## Writing Tests Tests are specified using the top-level [`test()`] function. Test asserts can be made using [`expect` from `package:matcher`][expect] [`test()`]: https://pub.dev/documentation/test/latest/test/test.html [expect]: https://pub.dev/documentation/matcher/latest/expect/expect.html ```dart 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'])); }); test('String.trim() removes surrounding whitespace', () { var string = ' foo '; expect(string.trim(), equals('foo')); }); } ``` Tests can be grouped together using the [`group()`] function. Each group's description is added to the beginning of its test's descriptions. [`group()`]: https://pub.dev/documentation/test/latest/test/group.html ```dart import 'package:test/test.dart'; void main() { group('String', () { test('.split() splits the string on the delimiter', () { var string = 'foo,bar,baz'; expect(string.split(','), equals(['foo', 'bar', 'baz'])); }); test('.trim() removes surrounding whitespace', () { var string = ' foo '; expect(string.trim(), equals('foo')); }); }); group('int', () { test('.remainder() returns the remainder of division', () { expect(11.remainder(3), equals(2)); }); test('.toRadixString() returns a hex string', () { expect(11.toRadixString(16), equals('b')); }); }); } ``` You can use the [`setUp()`] and [`tearDown()`] functions to share code between tests. The `setUp()` callback will run before every test in a group or test suite, and `tearDown()` will run after. `tearDown()` will run even if a test fails, to ensure that it has a chance to clean up after itself. ```dart import 'package:test/test.dart'; void main() { late HttpServer server; late Uri url; setUp(() async { server = await HttpServer.bind('localhost', 0); url = Uri.parse('http://${server.address.host}:${server.port}'); }); tearDown(() async { await server.close(force: true); server = null; url = null; }); // ... } ``` [`setUp()`]: https://pub.dev/documentation/test/latest/test/setUp.html [`tearDown()`]: https://pub.dev/documentation/test/latest/test/tearDown.html ## Running Tests A single test file can be run just using `dart test path/to/test.dart` (as of Dart 2.10 - prior sdk versions must use `pub run test` instead of `dart test`). ![Single file being run via "dart test"](https://raw.githubusercontent.com/dart-lang/test/master/pkgs/test/image/test1.gif) Many tests can be run at a time using `dart test path/to/dir`. ![Directory being run via "dart test".](https://raw.githubusercontent.com/dart-lang/test/master/pkgs/test/image/test2.gif) It's also possible to run a test on the Dart VM only by invoking it using `dart path/to/test.dart`, but this doesn't load the full test runner and will be missing some features. The test runner accepts one or more path arguments. If there are no path arguments the runner defaults to running tests under the `test/` directory. When running for `test/` or any other directory, the runner will recursively search the directory for files that match the test name pattern `*_test.dart`. The pattern can be overridden in `dart_test.yaml`. When a path argument is a file instead of a directory it will be run as a test, regardless of the file name. Arguments which use shell globbing should avoid including non-test files in the path argument. Tests which are run by other test runners may use a different default path, such as `integration_test/`. Directories other than `test/` are ignored by `dart test` unless passed explicitly as a path to run. You can select specific tests cases to run by name using `dart test -n "test name"`. The string is interpreted as a regular expression, and only tests whose description (including any group descriptions) match that regular expression will be run. You can also use the `-N` flag to run tests whose names contain a plain-text string. By default, tests are run in the Dart VM, but you can run them in the browser as well by passing `dart test -p chrome path/to/test.dart`. `test` will take care of starting the browser and loading the tests, and all the results will be reported on the command line just like for VM tests. In fact, you can even run tests on both platforms with a single command: `dart test -p "chrome,vm" path/to/test.dart`. By default each platform has a default compiler, but some of them support more than one compiler. You can choose which compiler to use by passing `dart test -c source`, which would run all VM tests from source instead of compiling them to kernel. This also supports targeting a specific platform using normal platform selectors, like this `dart test -c vm:source`. ### Test Path Queries Some query parameters are supported on test paths, which allow you to filter the tests that will run within just those paths. These filters are merged with any global options that are passed, and all filters must match for a test to be ran. - **name**: Works the same as `--name` (simple contains check). - This is the only option that supports more than one entry. - **full-name**: Requires an exact match for the name of the test. - **line**: Matches any test that originates from this line in the test suite. - **col**: Matches any test that originates from this column in the test suite. **Example Usage**: `dart test "path/to/test.dart?line=10&col=2"` #### Line/Col Matching Semantics The `line` and `col` filters match against the current stack trace taken from the invocation to the `test` function, and are considered a match if **any frame** in the trace meets **all** of the following criteria: * The URI of the frame matches the root test suite uri. * This means it will not match lines from imported libraries. * If both `line` and `col` are passed, both must match **the same frame**. * The specific `line` and `col` to be matched are defined by the tools creating the stack trace. This generally means they are 1 based and not 0 based, but this package is not in control of the exact semantics and they may vary based on platform implementations. ### Sharding Tests Tests can also be sharded with the `--total-shards` and `--shard-index` arguments, allowing you to split up your test suites and run them separately. For example, if you wanted to run 3 shards of your test suite, you could run them as follows: ```bash dart test --total-shards 3 --shard-index 0 path/to/test.dart dart test --total-shards 3 --shard-index 1 path/to/test.dart dart test --total-shards 3 --shard-index 2 path/to/test.dart ``` Sharding: This refers to the process of splitting up a large test suite into smaller subsets (called shards) that can be run independently. Sharding is particularly useful for distributed testing, where multiple machines are used to run tests simultaneously. By dividing the test suite into smaller subsets, you can run tests in parallel across multiple machines, which can significantly reduce the overall testing time. ### Test concurrency Test suites run concurrently by default, using half of the host's CPU cores. Use `--concurrency` to control the number of test suites that runs concurrently, meaning that multiple tests in independent suites or platforms can run at the same time. For example, if you wanted to run the tests on 4 threads, you could run the tests as follows: ```bash dart test --concurrency=4 ``` This can speed up the overall testing process, especially if you have a large number of test suites. ### Shuffling Tests Test order can be shuffled with the `--test-randomize-ordering-seed` argument. This allows you to shuffle your tests with a specific seed (deterministic) or a random seed for each run. For example, consider the following test runs: ```bash dart test --test-randomize-ordering-seed=12345 dart test --test-randomize-ordering-seed=random ``` Setting `--test-randomize-ordering-seed=0` will have the same effect as not specifying it at all, meaning the test order will remain as-is. ### Selecting a Test Reporter You can adjust the output format of test results using the `--reporter=