Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Side by Side Diff: test/runner_test.dart

Issue 934413002: Replace the existing unittest APIs with the new runner infrastructure. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'dart:io';
6
7 import 'package:path/path.dart' as p;
8 import 'package:unittest/src/exit_codes.dart' as exit_codes;
9 import 'package:unittest/unittest.dart';
10
11 import 'io.dart';
12
13 String _sandbox;
14
15 final _success = """
16 import 'dart:async';
17
18 import 'package:unittest/unittest.dart';
19
20 void main() {
21 test("success", () {});
22 }
23 """;
24
25 final _failure = """
26 import 'dart:async';
27
28 import 'package:unittest/unittest.dart';
29
30 void main() {
31 test("failure", () => throw new TestFailure("oh no"));
32 }
33 """;
34
35 void main() {
36 setUp(() {
37 _sandbox = Directory.systemTemp.createTempSync('unittest_').path;
38 });
39
40 tearDown(() {
41 new Directory(_sandbox).deleteSync(recursive: true);
42 });
43
44 test("prints help information", () {
45 var result = _runUnittest(["--help"]);
46 expect(result.stdout, equals("""
47 Runs tests in this package.
48
49 Usage: pub run unittest:unittest [files or directories...]
50
51 -h, --help Shows this usage information.
52 """));
53 expect(result.exitCode, equals(exit_codes.success));
54 });
55
56 group("fails gracefully if", () {
57 test("an invalid option is passed", () {
58 var result = _runUnittest(["--asdf"]);
59 expect(result.stderr, equals("""
60 Could not find an option named "asdf".
61
62 Usage: pub run unittest:unittest [files or directories...]
63
64 -h, --help Shows this usage information.
65 """));
66 expect(result.exitCode, equals(exit_codes.usage));
67 });
68
69 test("a non-existent file is passed", () {
70 var result = _runUnittest(["file"]);
71 expect(result.stderr, equals('Failed to load "file": Does not exist.\n'));
72 expect(result.exitCode, equals(exit_codes.data));
73 });
74
75 test("the default directory doesn't exist", () {
76 var result = _runUnittest([]);
77 expect(result.stderr, equals(
78 'Failed to load "test": No test files were passed and the default '
79 'directory doesn\'t exist.\n'));
80 expect(result.exitCode, equals(exit_codes.data));
81 });
82
83 test("a test file fails to load", () {
84 var testPath = p.join(_sandbox, "test.dart");
85 new File(testPath).writeAsStringSync("invalid Dart file");
86 var result = _runUnittest(["test.dart"]);
87
88 expect(result.stderr, equals(
89 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n'
90 "line 1 pos 1: unexpected token 'invalid'\n"
91 "invalid Dart file\n"
92 "^\n"));
93 expect(result.exitCode, equals(exit_codes.data));
94 });
95
96 test("a test file throws", () {
97 var testPath = p.join(_sandbox, "test.dart");
98 new File(testPath).writeAsStringSync("void main() => throw 'oh no';");
99
100 var result = _runUnittest(["test.dart"]);
101 expect(result.stderr, startsWith(
102 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no\n'));
103 expect(result.exitCode, equals(exit_codes.data));
104 });
105
106 test("a test file doesn't have a main defined", () {
107 var testPath = p.join(_sandbox, "test.dart");
108 new File(testPath).writeAsStringSync("void foo() {}");
109
110 var result = _runUnittest(["test.dart"]);
111 expect(result.stderr, startsWith(
112 'Failed to load "${p.relative(testPath, from: _sandbox)}": No '
113 'top-level main() function defined.\n'));
114 expect(result.exitCode, equals(exit_codes.data));
115 });
116
117 test("a test file has a non-function main", () {
118 var testPath = p.join(_sandbox, "test.dart");
119 new File(testPath).writeAsStringSync("int main;");
120
121 var result = _runUnittest(["test.dart"]);
122 expect(result.stderr, startsWith(
123 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level '
124 'main getter is not a function.\n'));
125 expect(result.exitCode, equals(exit_codes.data));
126 });
127
128 test("a test file has a main with arguments", () {
129 var testPath = p.join(_sandbox, "test.dart");
130 new File(testPath).writeAsStringSync("void main(arg) {}");
131
132 var result = _runUnittest(["test.dart"]);
133 expect(result.stderr, startsWith(
134 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level '
135 'main() function takes arguments.\n'));
136 expect(result.exitCode, equals(exit_codes.data));
137 });
138
139 // TODO(nweiz): test what happens when a test file is unreadable once issue
140 // 15078 is fixed.
141 });
142
143 group("runs successful tests", () {
144 test("defined in a single file", () {
145 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success);
146 var result = _runUnittest(["test.dart"]);
147 expect(result.exitCode, equals(0));
148 });
149
150 test("defined in a directory", () {
151 for (var i = 0; i < 3; i++) {
152 new File(p.join(_sandbox, "${i}_test.dart"))
153 .writeAsStringSync(_success);
154 }
155
156 var result = _runUnittest(["."]);
157 expect(result.exitCode, equals(0));
158 });
159
160 test("defaulting to the test directory", () {
161 new Directory(p.join(_sandbox, "test")).createSync();
162 for (var i = 0; i < 3; i++) {
163 new File(p.join(_sandbox, "test", "${i}_test.dart"))
164 .writeAsStringSync(_success);
165 }
166
167 var result = _runUnittest([]);
168 expect(result.exitCode, equals(0));
169 });
170
171 test("directly", () {
172 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success);
173 var result = _runDart([
174 "--package-root=${p.join(packageDir, 'packages')}",
175 "test.dart"
176 ]);
177 expect(result.stdout, contains("All tests passed!"));
178 });
179 });
180
181 group("runs failing tests", () {
182 test("defined in a single file", () {
183 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure);
184 var result = _runUnittest(["test.dart"]);
185 expect(result.exitCode, equals(1));
186 });
187
188 test("defined in a directory", () {
189 for (var i = 0; i < 3; i++) {
190 new File(p.join(_sandbox, "${i}_test.dart"))
191 .writeAsStringSync(_failure);
192 }
193
194 var result = _runUnittest(["."]);
195 expect(result.exitCode, equals(1));
196 });
197
198 test("defaulting to the test directory", () {
199 new Directory(p.join(_sandbox, "test")).createSync();
200 for (var i = 0; i < 3; i++) {
201 new File(p.join(_sandbox, "test", "${i}_test.dart"))
202 .writeAsStringSync(_failure);
203 }
204
205 var result = _runUnittest([]);
206 expect(result.exitCode, equals(1));
207 });
208
209 test("directly", () {
210 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure);
211 var result = _runDart([
212 "--package-root=${p.join(packageDir, 'packages')}",
213 "test.dart"
214 ]);
215 expect(result.stdout, contains("Some tests failed."));
216 });
217 });
218 }
219
220 ProcessResult _runUnittest(List<String> args) =>
221 runUnittest(args, workingDirectory: _sandbox);
222
223 ProcessResult _runDart(List<String> args) =>
224 runDart(args, workingDirectory: _sandbox);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698