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 /** | 5 /** |
6 * A library for writing dart unit tests. | 6 * Support for writing Dart unit tests. |
7 * | 7 * |
8 * ## Installing ## | 8 * For information on installing and importing this library, see the |
| 9 * [unittest package on pub.dartlang.org] |
| 10 * (http://pub.dartlang.org/packages/unittest). |
9 * | 11 * |
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` | 12 * **See also:** |
11 * file. | 13 * [Unit Testing with Dart] |
| 14 * (http://www.dartlang.org/articles/dart-unit-tests/) |
12 * | 15 * |
13 * dependencies: | 16 * ##Concepts |
14 * unittest: any | |
15 * | |
16 * Then run `pub install`. | |
17 * | |
18 * For more information, see the | |
19 * [unittest package on pub.dartlang.org][pkg]. | |
20 * | |
21 * See the [Getting Started](http://pub.dartlang.org/doc) | |
22 * guide for more details. | |
23 * | |
24 * ##Concepts## | |
25 * | 17 * |
26 * * __Tests__: Tests are specified via the top-level function [test], they can
be | 18 * * __Tests__: Tests are specified via the top-level function [test], they can
be |
27 * organized together using [group]. | 19 * organized together using [group]. |
| 20 * |
28 * * __Checks__: Test expectations can be specified via [expect] | 21 * * __Checks__: Test expectations can be specified via [expect] |
| 22 * |
29 * * __Matchers__: [expect] assertions are written declaratively using the | 23 * * __Matchers__: [expect] assertions are written declaratively using the |
30 * [Matcher] class. | 24 * [Matcher] class. |
| 25 * |
31 * * __Configuration__: The framework can be adapted by setting | 26 * * __Configuration__: The framework can be adapted by setting |
32 * [unittestConfiguration] with a [Configuration]. See the other libraries | 27 * [unittestConfiguration] with a [Configuration]. See the other libraries |
33 * in the `unittest` package for alternative implementations of | 28 * in the `unittest` package for alternative implementations of |
34 * [Configuration] including `compact_vm_config.dart`, `html_config.dart` and | 29 * [Configuration] including `compact_vm_config.dart`, `html_config.dart` and |
35 * `html_enhanced_config.dart`. | 30 * `html_enhanced_config.dart`. |
36 * | 31 * |
37 * ##Examples## | 32 * ##Examples |
38 * | 33 * |
39 * A trivial test: | 34 * A trivial test: |
40 * | 35 * |
41 * import 'package:unittest/unittest.dart'; | 36 * import 'package:unittest/unittest.dart'; |
42 * main() { | 37 * main() { |
43 * test('this is a test', () { | 38 * test('this is a test', () { |
44 * int x = 2 + 3; | 39 * int x = 2 + 3; |
45 * expect(x, equals(5)); | 40 * expect(x, equals(5)); |
46 * }); | 41 * }); |
47 * } | 42 * } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 * returns false the test will still be considered incomplete. | 119 * returns false the test will still be considered incomplete. |
125 * | 120 * |
126 * Test functions can return [Future]s, which provide another way of doing | 121 * Test functions can return [Future]s, which provide another way of doing |
127 * asynchronous tests. The test framework will handle exceptions thrown by | 122 * asynchronous tests. The test framework will handle exceptions thrown by |
128 * the Future, and will advance to the next test when the Future is complete. | 123 * the Future, and will advance to the next test when the Future is complete. |
129 * It is still important to use expectAsync/guardAsync with any parts of the | 124 * It is still important to use expectAsync/guardAsync with any parts of the |
130 * test that may be invoked from a top level context (for example, with | 125 * test that may be invoked from a top level context (for example, with |
131 * Timer.run()], as the Future exception handler may not capture exceptions | 126 * Timer.run()], as the Future exception handler may not capture exceptions |
132 * in such code. | 127 * in such code. |
133 * | 128 * |
134 * Note: due to some language limitations we have to use different functions | 129 * Note: Due to some language limitations we have to use different functions |
135 * depending on the number of positional arguments of the callback. In the | 130 * depending on the number of positional arguments of the callback. In the |
136 * future, we plan to expose a single `expectAsync` function that can be used | 131 * future, we plan to expose a single `expectAsync` function that can be used |
137 * regardless of the number of positional arguments. This requires new langauge | 132 * regardless of the number of positional arguments. This requires new langauge |
138 * features or fixes to the current spec (e.g. see | 133 * features or fixes to the current spec (e.g. see |
139 * [Issue 2706](http://dartbug.com/2706)). | 134 * [Issue 2706](http://dartbug.com/2706)). |
140 * | |
141 * [pub]: http://pub.dartlang.org | |
142 * [pkg]: http://pub.dartlang.org/packages/unittest | |
143 */ | 135 */ |
144 library unittest; | 136 library unittest; |
145 | 137 |
146 import 'dart:async'; | 138 import 'dart:async'; |
147 import 'dart:collection'; | 139 import 'dart:collection'; |
148 import 'dart:isolate'; | 140 import 'dart:isolate'; |
149 import 'package:stack_trace/stack_trace.dart'; | 141 import 'package:stack_trace/stack_trace.dart'; |
150 | 142 |
151 import 'matcher.dart'; | 143 import 'matcher.dart'; |
152 export 'matcher.dart'; | 144 export 'matcher.dart'; |
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 | 880 |
889 if (!filterStacks) return trace; | 881 if (!filterStacks) return trace; |
890 | 882 |
891 // Format the stack trace by removing everything above TestCase._runTest, | 883 // Format the stack trace by removing everything above TestCase._runTest, |
892 // which is usually going to be irrelevant. Also fold together unittest and | 884 // which is usually going to be irrelevant. Also fold together unittest and |
893 // core library calls so only the function the user called is visible. | 885 // core library calls so only the function the user called is visible. |
894 return new Trace(trace.frames.takeWhile((frame) { | 886 return new Trace(trace.frames.takeWhile((frame) { |
895 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; | 887 return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; |
896 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); | 888 })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); |
897 } | 889 } |
OLD | NEW |