OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Support for writing Dart unit tests. | |
6 /// | |
7 /// For information on installing and importing this library, see the | |
8 /// [unittest package on pub.dartlang.org] | |
9 /// (http://pub.dartlang.org/packages/unittest). | |
10 /// | |
11 /// **See also:** | |
12 /// [Unit Testing with Dart] | |
13 /// (http://www.dartlang.org/articles/dart-unit-tests/) | |
14 /// | |
15 /// ##Concepts | |
16 /// | |
17 /// * __Tests__: Tests are specified via the top-level function [test], they ca
n be | |
18 /// organized together using [group]. | |
19 /// | |
20 /// * __Checks__: Test expectations can be specified via [expect] | |
21 /// | |
22 /// * __Matchers__: [expect] assertions are written declaratively using the | |
23 /// [Matcher] class. | |
24 /// | |
25 /// * __Configuration__: The framework can be adapted by setting | |
26 /// [unittestConfiguration] with a [Configuration]. See the other libraries | |
27 /// in the `unittest` package for alternative implementations of | |
28 /// [Configuration] including `compact_vm_config.dart`, `html_config.dart` | |
29 /// and `html_enhanced_config.dart`. | |
30 /// | |
31 /// ##Examples | |
32 /// | |
33 /// A trivial test: | |
34 /// | |
35 /// import 'package:unittest/unittest.dart'; | |
36 /// main() { | |
37 /// test('this is a test', () { | |
38 /// int x = 2 + 3; | |
39 /// expect(x, equals(5)); | |
40 /// }); | |
41 /// } | |
42 /// | |
43 /// Multiple tests: | |
44 /// | |
45 /// import 'package:unittest/unittest.dart'; | |
46 /// main() { | |
47 /// test('this is a test', () { | |
48 /// int x = 2 + 3; | |
49 /// expect(x, equals(5)); | |
50 /// }); | |
51 /// test('this is another test', () { | |
52 /// int x = 2 + 3; | |
53 /// expect(x, equals(5)); | |
54 /// }); | |
55 /// } | |
56 /// | |
57 /// Multiple tests, grouped by category: | |
58 /// | |
59 /// import 'package:unittest/unittest.dart'; | |
60 /// 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 /// Asynchronous tests: if callbacks expect between 0 and 6 positional | |
80 /// arguments, [expectAsync] will wrap a function into a new callback and will | |
81 /// not consider the test complete until that callback is run. A count argument | |
82 /// can be provided to specify the number of times the callback should be called | |
83 /// (the default is 1). | |
84 /// | |
85 /// import 'dart:async'; | |
86 /// import 'package:unittest/unittest.dart'; | |
87 /// void main() { | |
88 /// test('callback is executed once', () { | |
89 /// // wrap the callback of an asynchronous call with [expectAsync] if | |
90 /// // the callback takes 0 arguments... | |
91 /// var timer = Timer.run(expectAsync(() { | |
92 /// int x = 2 + 3; | |
93 /// expect(x, equals(5)); | |
94 /// })); | |
95 /// }); | |
96 /// | |
97 /// test('callback is executed twice', () { | |
98 /// var callback = expectAsync(() { | |
99 /// int x = 2 + 3; | |
100 /// expect(x, equals(5)); | |
101 /// }, count: 2); // <-- we can indicate multiplicity to [expectAsync] | |
102 /// Timer.run(callback); | |
103 /// Timer.run(callback); | |
104 /// }); | |
105 /// } | |
106 /// | |
107 /// There may be times when the number of times a callback should be called is | |
108 /// non-deterministic. In this case a dummy callback can be created with | |
109 /// expectAsync((){}) and this can be called from the real callback when it is | |
110 /// finally complete. | |
111 /// | |
112 /// A variation on this is [expectAsyncUntil], which takes a callback as the | |
113 /// first parameter and a predicate function as the second parameter. After each | |
114 /// time the callback is called, the predicate function will be called. If it | |
115 /// returns `false` the test will still be considered incomplete. | |
116 /// | |
117 /// Test functions can return [Future]s, which provide another way of doing | |
118 /// asynchronous tests. The test framework will handle exceptions thrown by | |
119 /// the Future, and will advance to the next test when the Future is complete. | |
120 /// | |
121 /// import 'dart:async'; | |
122 /// import 'package:unittest/unittest.dart'; | |
123 /// void main() { | |
124 /// test('test that time has passed', () { | |
125 /// var duration = const Duration(milliseconds: 200); | |
126 /// var time = new DateTime.now(); | |
127 /// | |
128 /// return new Future.delayed(duration).then((_) { | |
129 /// var delta = new DateTime.now().difference(time); | |
130 /// | |
131 /// expect(delta, greaterThanOrEqualTo(duration)); | |
132 /// }); | |
133 /// }); | |
134 /// } | |
135 library unittest; | 5 library unittest; |
136 | 6 |
137 import 'dart:async'; | 7 import 'dart:async'; |
138 import 'dart:collection'; | 8 import 'dart:collection'; |
139 import 'dart:isolate'; | 9 import 'dart:isolate'; |
140 | 10 |
141 import 'package:matcher/matcher.dart' | 11 import 'package:matcher/matcher.dart' |
142 show | 12 show |
143 DefaultFailureHandler, | 13 DefaultFailureHandler, |
144 configureExpectFailureHandler, | 14 configureExpectFailureHandler, |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 /// | 473 /// |
604 /// This allows for multiple invocations of the unittest library in the same | 474 /// This allows for multiple invocations of the unittest library in the same |
605 /// application instance. | 475 /// application instance. |
606 /// This is useful when, for example, creating a test runner application which | 476 /// This is useful when, for example, creating a test runner application which |
607 /// needs to create a new pristine test environment on each invocation to run | 477 /// needs to create a new pristine test environment on each invocation to run |
608 /// a given set of test. | 478 /// a given set of test. |
609 dynamic withTestEnvironment(callback()) { | 479 dynamic withTestEnvironment(callback()) { |
610 return runZoned(callback, | 480 return runZoned(callback, |
611 zoneValues: {_UNITTEST_ENVIRONMENT: new _TestEnvironment()}); | 481 zoneValues: {_UNITTEST_ENVIRONMENT: new _TestEnvironment()}); |
612 } | 482 } |
OLD | NEW |