OLD | NEW |
(Empty) | |
| 1 Support for writing unit tests in Dart. |
| 2 |
| 3 **See also:** |
| 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 main() { |
| 30 test('this is a test', () { |
| 31 int x = 2 + 3; |
| 32 expect(x, equals(5)); |
| 33 }); |
| 34 } |
| 35 ``` |
| 36 |
| 37 Multiple tests: |
| 38 |
| 39 ```dart |
| 40 import 'package:unittest/unittest.dart'; |
| 41 main() { |
| 42 test('this is a test', () { |
| 43 int x = 2 + 3; |
| 44 expect(x, equals(5)); |
| 45 }); |
| 46 test('this is another test', () { |
| 47 int x = 2 + 3; |
| 48 expect(x, equals(5)); |
| 49 }); |
| 50 } |
| 51 ``` |
| 52 |
| 53 Multiple tests, grouped by category: |
| 54 |
| 55 ```dart |
| 56 import 'package:unittest/unittest.dart'; |
| 57 main() { |
| 58 group('group A', () { |
| 59 test('test A.1', () { |
| 60 int x = 2 + 3; |
| 61 expect(x, equals(5)); |
| 62 }); |
| 63 test('test A.2', () { |
| 64 int x = 2 + 3; |
| 65 expect(x, equals(5)); |
| 66 }); |
| 67 }); |
| 68 group('group B', () { |
| 69 test('this B.1', () { |
| 70 int x = 2 + 3; |
| 71 expect(x, equals(5)); |
| 72 }); |
| 73 }); |
| 74 } |
| 75 ``` |
| 76 |
| 77 Asynchronous tests: if callbacks expect between 0 and 6 positional |
| 78 arguments, [expectAsync] will wrap a function into a new callback and will |
| 79 not consider the test complete until that callback is run. A count argument |
| 80 can be provided to specify the number of times the callback should be called |
| 81 (the default is 1). |
| 82 |
| 83 ```dart |
| 84 import 'dart:async'; |
| 85 import 'package:unittest/unittest.dart'; |
| 86 void main() { |
| 87 test('callback is executed once', () { |
| 88 // wrap the callback of an asynchronous call with [expectAsync] if |
| 89 // the callback takes 0 arguments... |
| 90 var timer = Timer.run(expectAsync(() { |
| 91 int x = 2 + 3; |
| 92 expect(x, equals(5)); |
| 93 })); |
| 94 }); |
| 95 |
| 96 test('callback is executed twice', () { |
| 97 var callback = expectAsync(() { |
| 98 int x = 2 + 3; |
| 99 expect(x, equals(5)); |
| 100 }, count: 2); // <-- we can indicate multiplicity to [expectAsync] |
| 101 Timer.run(callback); |
| 102 Timer.run(callback); |
| 103 }); |
| 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 ```dart |
| 122 import 'dart:async'; |
| 123 import 'package:unittest/unittest.dart'; |
| 124 void main() { |
| 125 test('test that time has passed', () { |
| 126 var duration = const Duration(milliseconds: 200); |
| 127 var time = new DateTime.now(); |
| 128 |
| 129 return new Future.delayed(duration).then((_) { |
| 130 var delta = new DateTime.now().difference(time); |
| 131 |
| 132 expect(delta, greaterThanOrEqualTo(duration)); |
| 133 }); |
| 134 }); |
| 135 } |
| 136 ``` |
| 137 |
OLD | NEW |