OLD | NEW |
1 Support for writing unit tests in Dart. | 1 The `unittest` package has been renamed [`test`][test]. It will export `test`'s |
| 2 API through the `0.12.x` branch, but it is deprecated and `test` should be used |
| 3 instead. |
2 | 4 |
3 **See also:** | 5 [test]: https://pub.dartlang.org/packages/test |
4 [Unit Testing with Dart] | |
5 (http://www.dartlang.org/articles/dart-unit-tests/) | |
6 | |
7 ##Concepts | |
8 | |
9 * __Tests__: Tests are specified via the top-level function [test], they can be | |
10 organized together using [group]. | |
11 | |
12 * __Checks__: Test expectations can be specified via [expect] | |
13 | |
14 * __Matchers__: [expect] assertions are written declaratively using the | |
15 [Matcher] class. | |
16 | |
17 * __Configuration__: The framework can be adapted by setting | |
18 [unittestConfiguration] with a [Configuration]. See the other libraries | |
19 in the `unittest` package for alternative implementations of | |
20 [Configuration] including `compact_vm_config.dart`, `html_config.dart` | |
21 and `html_enhanced_config.dart`. | |
22 | |
23 ##Examples | |
24 | |
25 A trivial test: | |
26 | |
27 ```dart | |
28 import 'package:unittest/unittest.dart'; | |
29 | |
30 void main() { | |
31 test('this is a test', () { | |
32 int x = 2 + 3; | |
33 expect(x, equals(5)); | |
34 }); | |
35 } | |
36 ``` | |
37 | |
38 Multiple tests: | |
39 | |
40 ```dart | |
41 import 'package:unittest/unittest.dart'; | |
42 | |
43 void main() { | |
44 test('this is a test', () { | |
45 int x = 2 + 3; | |
46 expect(x, equals(5)); | |
47 }); | |
48 test('this is another test', () { | |
49 int x = 2 + 3; | |
50 expect(x, equals(5)); | |
51 }); | |
52 } | |
53 ``` | |
54 | |
55 Multiple tests, grouped by category: | |
56 | |
57 ```dart | |
58 import 'package:unittest/unittest.dart'; | |
59 | |
60 void main() { | |
61 group('group A', () { | |
62 test('test A.1', () { | |
63 int x = 2 + 3; | |
64 expect(x, equals(5)); | |
65 }); | |
66 test('test A.2', () { | |
67 int x = 2 + 3; | |
68 expect(x, equals(5)); | |
69 }); | |
70 }); | |
71 group('group B', () { | |
72 test('this B.1', () { | |
73 int x = 2 + 3; | |
74 expect(x, equals(5)); | |
75 }); | |
76 }); | |
77 } | |
78 ``` | |
79 | |
80 Asynchronous tests: if callbacks expect between 0 and 6 positional | |
81 arguments, [expectAsync] will wrap a function into a new callback and will | |
82 not consider the test complete until that callback is run. A count argument | |
83 can be provided to specify the number of times the callback should be called | |
84 (the default is 1). | |
85 | |
86 ```dart | |
87 import 'dart:async'; | |
88 import 'package:unittest/unittest.dart'; | |
89 | |
90 void main() { | |
91 test('callback is executed once', () { | |
92 // wrap the callback of an asynchronous call with [expectAsync] if | |
93 // the callback takes 0 arguments... | |
94 Timer.run(expectAsync(() { | |
95 int x = 2 + 3; | |
96 expect(x, equals(5)); | |
97 })); | |
98 }); | |
99 | |
100 test('callback is executed twice', () { | |
101 var callback = expectAsync(() { | |
102 int x = 2 + 3; | |
103 expect(x, equals(5)); | |
104 }, count: 2); // <-- we can indicate multiplicity to [expectAsync] | |
105 Timer.run(callback); | |
106 Timer.run(callback); | |
107 }); | |
108 } | |
109 ``` | |
110 | |
111 There may be times when the number of times a callback should be called is | |
112 non-deterministic. In this case a dummy callback can be created with | |
113 expectAsync((){}) and this can be called from the real callback when it is | |
114 finally complete. | |
115 | |
116 A variation on this is [expectAsyncUntil], which takes a callback as the | |
117 first parameter and a predicate function as the second parameter. After each | |
118 time the callback is called, the predicate function will be called. If it | |
119 returns `false` the test will still be considered incomplete. | |
120 | |
121 Test functions can return [Future]s, which provide another way of doing | |
122 asynchronous tests. The test framework will handle exceptions thrown by | |
123 the Future, and will advance to the next test when the Future is complete. | |
124 | |
125 ```dart | |
126 import 'dart:async'; | |
127 import 'package:unittest/unittest.dart'; | |
128 | |
129 void main() { | |
130 test('test that time has passed', () { | |
131 var duration = const Duration(milliseconds: 200); | |
132 var time = new DateTime.now(); | |
133 | |
134 return new Future.delayed(duration).then((_) { | |
135 var delta = new DateTime.now().difference(time); | |
136 | |
137 expect(delta, greaterThanOrEqualTo(duration)); | |
138 }); | |
139 }); | |
140 } | |
141 ``` | |
OLD | NEW |