diff --git a/README.md b/README.md index 091b0107311fd2c00a8dcc9bedc89f184216bc6f..ea060a7824c1e8b5bac0f3561c19920a742721e3 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,35 @@ void main() { } ``` +You can use the [`setUp()`][setUp] and [`tearDown()`][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() { + var server; + var 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]: http://www.dartdocs.org/documentation/test/latest/index.html#test/test@id_setUp +[tearDown]: http://www.dartdocs.org/documentation/test/latest/index.html#test/test@id_tearDown + ## Running Tests A single test file can be run just using `pub run test:test path/to/test.dart`