From 9f8c52d2ee266e75fc4c952ef07483368bd2ff78 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum <nweiz@google.com> Date: Tue, 13 Oct 2015 13:10:55 -0700 Subject: [PATCH] Talk about setUp and tearDown in the README. Closes #347 R=kevmoo@google.com Review URL: https://codereview.chromium.org//1397903003 . --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 091b0107..ea060a78 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` -- GitLab