OLD | NEW |
1 `unittest` provides a standard way of writing and running tests in Dart. | 1 `unittest` 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/unittest/latest/index.html#unittes
t/unittest@id_test | 8 [test]: http://www.dartdocs.org/documentation/unittest/latest/index.html#unittes
t/unittest@id_test |
9 [expect]: http://www.dartdocs.org/documentation/unittest/latest/index.html#unitt
est/unittest@id_expect | 9 [expect]: http://www.dartdocs.org/documentation/unittest/latest/index.html#unitt
est/unittest@id_expect |
10 | 10 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 you don't pass any paths, it will run all the test files in your `test/` | 88 you don't pass any paths, it will run all the test files in your `test/` |
89 directory, making it easy to test your entire application at once. | 89 directory, making it easy to test your entire application at once. |
90 | 90 |
91 By default, tests are run in the Dart VM, but you can run them in the browser as | 91 By default, tests are run in the Dart VM, but you can run them in the browser as |
92 well by passing `pub run unittest:unittest -p chrome path/to/test.dart`. | 92 well by passing `pub run unittest:unittest -p chrome path/to/test.dart`. |
93 `unittest` will take care of starting the browser and loading the tests, and all | 93 `unittest` will take care of starting the browser and loading the tests, and all |
94 the results will be reported on the command line just like for VM tests. In | 94 the results will be reported on the command line just like for VM tests. In |
95 fact, you can even run tests on both platforms with a single command: `pub run | 95 fact, you can even run tests on both platforms with a single command: `pub run |
96 unittest:unittest -p chrome -p vm path/to/test.dart`. | 96 unittest:unittest -p chrome -p vm path/to/test.dart`. |
97 | 97 |
| 98 ### Restricting Tests to Certain Platforms |
| 99 |
| 100 Some test files only make sense to run on particular platforms. They may use |
| 101 `dart:html` or `dart:io`, they might test Windows' particular filesystem |
| 102 behavior, or they might use a feature that's only available in Chrome. The |
| 103 [`@TestOn`][TestOn] annotation makes it easy to declare exactly which platforms |
| 104 a test file should run on. Just put it at the top of your file, before any |
| 105 `library` or `import` declarations: |
| 106 |
| 107 ```dart |
| 108 @TestOn("vm") |
| 109 |
| 110 import "dart:io"; |
| 111 |
| 112 import "package:unittest/unittest.dart"; |
| 113 |
| 114 void main() { |
| 115 // ... |
| 116 } |
| 117 ``` |
| 118 |
| 119 [TestOn]: http://www.dartdocs.org/documentation/unittest/latest/index.html#unitt
est/unittest.TestOn |
| 120 |
| 121 The string you pass to `@TestOn` is what's called a "platform selector", and it |
| 122 specifies exactly which platforms a test can run on. It can be as simple as the |
| 123 name of a platform, or a more complex Dart-like boolean expression involving |
| 124 these platform names. |
| 125 |
| 126 ### Platform Selector Syntax |
| 127 |
| 128 Platform selectors can contain identifiers, parentheses, and operators. When |
| 129 loading a test, each identifier is set to `true` or `false` based on the current |
| 130 platform, and the test is only loaded if the platform selector returns `true`. |
| 131 The operators `||`, `&&`, `!`, and `? :` all work just like they do in Dart. The |
| 132 valid identifiers are: |
| 133 |
| 134 * `vm`: Whether the test is running on the command-line Dart VM. |
| 135 |
| 136 * `chrome`: Whether the test is running on Google Chrome. |
| 137 |
| 138 * `dart-vm`: Whether the test is running on the Dart VM in any context. For now |
| 139 this is identical to `vm`, but it will also be true for Dartium in the future. |
| 140 It's identical to `!js`. |
| 141 |
| 142 * `browser`: Whether the test is running in any browser. |
| 143 |
| 144 * `js`: Whether the test has been compiled to JS. This is identical to |
| 145 `!dart-vm`. |
| 146 |
| 147 * `blink`: Whether the test is running in a browser that uses the Blink |
| 148 rendering engine. |
| 149 |
| 150 * `windows`: Whether the test is running on Windows. If `vm` is false, this will |
| 151 be `false` as well. |
| 152 |
| 153 * `mac-os`: Whether the test is running on Mac OS. If `vm` is false, this will |
| 154 be `false` as well. |
| 155 |
| 156 * `linux`: Whether the test is running on Linux. If `vm` is false, this will be |
| 157 `false` as well. |
| 158 |
| 159 * `android`: Whether the test is running on Android. If `vm` is false, this will |
| 160 be `false` as well, which means that this *won't* be true if the test is |
| 161 running on an Android browser. |
| 162 |
| 163 * `posix`: Whether the test is running on a POSIX operating system. This is |
| 164 equivalent to `!windows`. |
| 165 |
| 166 For example, if you wanted to run a test on every browser but Chrome, you would |
| 167 write `@TestOn("browser && !chrome")`. |
| 168 |
98 ## Asynchronous Tests | 169 ## Asynchronous Tests |
99 | 170 |
100 Tests written with `async`/`await` will work automatically. The test runner | 171 Tests written with `async`/`await` will work automatically. The test runner |
101 won't consider the test finished until the returned `Future` completes. | 172 won't consider the test finished until the returned `Future` completes. |
102 | 173 |
103 ```dart | 174 ```dart |
104 import "dart:async"; | 175 import "dart:async"; |
105 | 176 |
106 import "package:unittest/unittest.dart"; | 177 import "package:unittest/unittest.dart"; |
107 | 178 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 var stream = new Stream.fromIterable([1, 2, 3]); | 238 var stream = new Stream.fromIterable([1, 2, 3]); |
168 | 239 |
169 stream.listen(expectAsync((number) { | 240 stream.listen(expectAsync((number) { |
170 expect(number, inInclusiveRange(1, 3)); | 241 expect(number, inInclusiveRange(1, 3)); |
171 }, count: 3)); | 242 }, count: 3)); |
172 }); | 243 }); |
173 } | 244 } |
174 ``` | 245 ``` |
175 | 246 |
176 [expectAsync]: http://www.dartdocs.org/documentation/unittest/latest/index.html#
unittest/unittest@id_expectAsync | 247 [expectAsync]: http://www.dartdocs.org/documentation/unittest/latest/index.html#
unittest/unittest@id_expectAsync |
OLD | NEW |