OLD | NEW |
1 `test` provides a standard way of writing and running tests in Dart. | 1 `test` provides a standard way of writing and running tests in Dart. |
2 | 2 |
3 ## Writing Tests | 3 ## Writing Tests |
4 | 4 |
5 Tests are specified using the top-level [`test()`][test] function, and test | 5 Tests are specified using the top-level [`test()`][test] function, and test |
6 assertions are made using [`expect()`][expect]: | 6 assertions are made using [`expect()`][expect]: |
7 | 7 |
8 [test]: http://www.dartdocs.org/documentation/test/latest/index.html#test/test@i
d_test | 8 [test]: http://www.dartdocs.org/documentation/test/latest/index.html#test/test@i
d_test |
9 [expect]: http://www.dartdocs.org/documentation/test/latest/index.html#test/test
@id_expect | 9 [expect]: http://www.dartdocs.org/documentation/test/latest/index.html#test/test
@id_expect |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 test(".split() splits the string on the delimiter", () { | 69 test(".split() splits the string on the delimiter", () { |
70 expect("foo,bar,baz", allOf([ | 70 expect("foo,bar,baz", allOf([ |
71 contains("foo"), | 71 contains("foo"), |
72 isNot(startsWith("bar")), | 72 isNot(startsWith("bar")), |
73 endsWith("baz") | 73 endsWith("baz") |
74 ])); | 74 ])); |
75 }); | 75 }); |
76 } | 76 } |
77 ``` | 77 ``` |
78 | 78 |
| 79 You can use the [`setUp()`][setUp] and [`tearDown()`][tearDown] functions to |
| 80 share code between tests. The `setUp()` callback will run before every test in a |
| 81 group or test suite, and `tearDown()` will run after. `tearDown()` will run even |
| 82 if a test fails, to ensure that it has a chance to clean up after itself. |
| 83 |
| 84 ```dart |
| 85 import "package:test/test.dart"; |
| 86 |
| 87 void main() { |
| 88 var server; |
| 89 var url; |
| 90 setUp(() async { |
| 91 server = await HttpServer.bind('localhost', 0); |
| 92 url = Uri.parse("http://${server.address.host}:${server.port}"); |
| 93 }); |
| 94 |
| 95 tearDown(() async { |
| 96 await server.close(force: true); |
| 97 server = null; |
| 98 url = null; |
| 99 }); |
| 100 |
| 101 // ... |
| 102 } |
| 103 ``` |
| 104 |
| 105 [setUp]: http://www.dartdocs.org/documentation/test/latest/index.html#test/test@
id_setUp |
| 106 [tearDown]: http://www.dartdocs.org/documentation/test/latest/index.html#test/te
st@id_tearDown |
| 107 |
79 ## Running Tests | 108 ## Running Tests |
80 | 109 |
81 A single test file can be run just using `pub run test:test path/to/test.dart` | 110 A single test file can be run just using `pub run test:test path/to/test.dart` |
82 (on Dart 1.10, this can be shortened to `pub run test path/to/test.dart`). | 111 (on Dart 1.10, this can be shortened to `pub run test path/to/test.dart`). |
83 | 112 |
84 ![Single file being run via pub run"](https://raw.githubusercontent.com/dart-lan
g/test/master/image/test1.gif) | 113 ![Single file being run via pub run"](https://raw.githubusercontent.com/dart-lan
g/test/master/image/test1.gif) |
85 | 114 |
86 Many tests can be run at a time using `pub run test:test path/to/dir`. | 115 Many tests can be run at a time using `pub run test:test path/to/dir`. |
87 | 116 |
88 ![Directory being run via "pub run".](https://raw.githubusercontent.com/dart-lan
g/test/master/image/test2.gif) | 117 ![Directory being run via "pub run".](https://raw.githubusercontent.com/dart-lan
g/test/master/image/test2.gif) |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 | 515 |
487 In this case, the port is `8081`. In another terminal, pass this port to | 516 In this case, the port is `8081`. In another terminal, pass this port to |
488 `--pub-serve` and otherwise invoke `pub run test:test` as normal: | 517 `--pub-serve` and otherwise invoke `pub run test:test` as normal: |
489 | 518 |
490 ```shell | 519 ```shell |
491 $ pub run test:test --pub-serve=8081 -p chrome | 520 $ pub run test:test --pub-serve=8081 -p chrome |
492 "pub serve" is compiling test/my_app_test.dart... | 521 "pub serve" is compiling test/my_app_test.dart... |
493 "pub serve" is compiling test/utils_test.dart... | 522 "pub serve" is compiling test/utils_test.dart... |
494 00:00 +42: All tests passed! | 523 00:00 +42: All tests passed! |
495 ``` | 524 ``` |
OLD | NEW |