/
github_reporter_test.dart
346 lines (312 loc) · 10 KB
/
github_reporter_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@TestOn('vm')
import 'dart:async';
import 'package:test_descriptor/test_descriptor.dart' as d;
import 'package:test/test.dart';
import '../io.dart';
void main() {
setUpAll(precompileTestExecutable);
test('reports when no tests are run', () async {
await d.file('test.dart', 'void main() {}').create();
var test = await runTest(['test.dart'], reporter: 'github');
expect(test.stdout, emitsThrough(contains('0 tests passed')));
await test.shouldExit(79);
});
test('runs several successful tests and reports when each completes', () {
return _expectReport('''
test('success 1', () {});
test('success 2', () {});
test('success 3', () {});''', '''
::group::✅ success 1
::endgroup::
::group::✅ success 2
::endgroup::
::group::✅ success 3
::endgroup::
🎉 3 tests passed.''');
});
test('runs several failing tests and reports when each fails', () {
return _expectReport('''
test('failure 1', () => throw TestFailure('oh no'));
test('failure 2', () => throw TestFailure('oh no'));
test('failure 3', () => throw TestFailure('oh no'));''', '''
::group::❌ failure 1 (failed)
oh no
test.dart 6:33 main.<fn>
::endgroup::
::group::❌ failure 2 (failed)
oh no
test.dart 7:33 main.<fn>
::endgroup::
::group::❌ failure 3 (failed)
oh no
test.dart 8:33 main.<fn>
::endgroup::
::error::0 tests passed, 3 failed.''');
});
test('includes the full stack trace with --verbose-trace', () async {
await d.file('test.dart', '''
import 'dart:async';
import 'package:test/test.dart';
void main() {
test("failure", () => throw "oh no");
}
''').create();
var test =
await runTest(['--verbose-trace', 'test.dart'], reporter: 'github');
expect(test.stdout, emitsThrough(contains('dart:async')));
await test.shouldExit(1);
});
test('runs failing tests along with successful tests', () {
return _expectReport('''
test('failure 1', () => throw TestFailure('oh no'));
test('success 1', () {});
test('failure 2', () => throw TestFailure('oh no'));
test('success 2', () {});''', '''
::group::❌ failure 1 (failed)
oh no
test.dart 6:33 main.<fn>
::endgroup::
::group::✅ success 1
::endgroup::
::group::❌ failure 2 (failed)
oh no
test.dart 8:33 main.<fn>
::endgroup::
::group::✅ success 2
::endgroup::
::error::2 tests passed, 2 failed.''');
});
test('always prints the full test name', () {
return _expectReport(
'''
test(
'really gosh dang long test name. Even longer than that. No, yet '
'longer. A little more... okay, that should do it.',
() {});''',
'''
::group::✅ really gosh dang long test name. Even longer than that. No, yet longer. A little more... okay, that should do it.
::endgroup::''',
useContains: true,
);
});
test('gracefully handles multiple test failures in a row', () {
return _expectReport('''
// This completer ensures that the test isolate isn't killed until all
// errors have been thrown.
var completer = Completer();
test('failures', () {
Future.microtask(() => throw 'first error');
Future.microtask(() => throw 'second error');
Future.microtask(() => throw 'third error');
Future.microtask(completer.complete);
});
test('wait', () => completer.future);''', '''
::group::❌ failures (failed)
first error
test.dart 10:34 main.<fn>.<fn>
second error
test.dart 11:34 main.<fn>.<fn>
third error
test.dart 12:34 main.<fn>.<fn>
::endgroup::
::group::✅ wait
::endgroup::
::error::1 test passed, 1 failed.''');
});
group('print:', () {
test('handles multiple prints', () {
return _expectReport(
'''
test('test', () {
print("one");
print("two");
print("three");
print("four");
});''',
'''
::group::✅ test
one
two
three
four
::endgroup::''',
useContains: true,
);
});
test('handles a print after the test completes', () {
return _expectReport('''
// This completer ensures that the test isolate isn't killed until all
// prints have happened.
var testDone = Completer();
var waitStarted = Completer();
test('test', () async {
waitStarted.future.then((_) {
Future(() => print("one"));
Future(() => print("two"));
Future(() => print("three"));
Future(() => print("four"));
Future(testDone.complete);
});
});
test('wait', () {
waitStarted.complete();
return testDone.future;
});''', '''
::group::✅ test
::endgroup::
one
two
three
four
::group::✅ wait
::endgroup::
🎉 2 tests passed.''');
});
});
group('skip:', () {
test('displays skipped tests', () {
return _expectReport('''
test('skip 1', () {}, skip: true);
test('skip 2', () {}, skip: true);
test('skip 3', () {}, skip: true);''', '''
::group::❎ skip 1 (skipped)
::endgroup::
::group::❎ skip 2 (skipped)
::endgroup::
::group::❎ skip 3 (skipped)
::endgroup::
🎉 0 tests passed, 3 skipped.''');
});
test('displays a skipped group', () {
return _expectReport('''
group('skip', () {
test('test 1', () {});
test('test 2', () {});
test('test 3', () {});
}, skip: true);''', '''
::group::❎ skip test 1 (skipped)
::endgroup::
::group::❎ skip test 2 (skipped)
::endgroup::
::group::❎ skip test 3 (skipped)
::endgroup::
🎉 0 tests passed, 3 skipped.''');
});
test('runs skipped tests along with successful tests', () {
return _expectReport('''
test('skip 1', () {}, skip: true);
test('success 1', () {});
test('skip 2', () {}, skip: true);
test('success 2', () {});''', '''
::group::❎ skip 1 (skipped)
::endgroup::
::group::✅ success 1
::endgroup::
::group::❎ skip 2 (skipped)
::endgroup::
::group::✅ success 2
::endgroup::
🎉 2 tests passed, 2 skipped.''');
});
test('runs skipped tests along with successful and failing tests', () {
return _expectReport('''
test('failure 1', () => throw TestFailure('oh no'));
test('skip 1', () {}, skip: true);
test('success 1', () {});
test('failure 2', () => throw TestFailure('oh no'));
test('skip 2', () {}, skip: true);
test('success 2', () {});''', '''
::group::❌ failure 1 (failed)
oh no
test.dart 6:35 main.<fn>
::endgroup::
::group::❎ skip 1 (skipped)
::endgroup::
::group::✅ success 1
::endgroup::
::group::❌ failure 2 (failed)
oh no
test.dart 9:35 main.<fn>
::endgroup::
::group::❎ skip 2 (skipped)
::endgroup::
::group::✅ success 2
::endgroup::
::error::2 tests passed, 2 failed, 2 skipped.''');
});
test('displays the skip reason if available', () {
return _expectReport('''
test('skip 1', () {}, skip: 'some reason');
test('skip 2', () {}, skip: 'or another');''', '''
::group::❎ skip 1 (skipped)
Skip: some reason
::endgroup::
::group::❎ skip 2 (skipped)
Skip: or another
::endgroup::
🎉 0 tests passed, 2 skipped.''');
});
});
test('loadSuite, setUpAll, and tearDownAll hidden if no content', () {
return _expectReport('''
group('one', () {
setUpAll(() {/* nothing to do here */});
tearDownAll(() {/* nothing to do here */});
test('test 1', () {});
});''', '''
::group::✅ one test 1
::endgroup::
🎉 1 test passed.''');
});
test('setUpAll and tearDownAll show when they have content', () {
return _expectReport('''
group('one', () {
setUpAll(() { print('one'); });
tearDownAll(() { print('two'); });
test('test 1', () {});
});''', '''
::group::⏺ one (setUpAll)
one
::endgroup::
::group::✅ one test 1
::endgroup::
::group::⏺ one (tearDownAll)
two
::endgroup::
🎉 1 test passed.''');
});
}
Future<void> _expectReport(
String tests,
String expected, {
List<String> args = const [],
bool useContains = false,
}) async {
await d.file('test.dart', '''
import 'dart:async';
import 'package:test/test.dart';
void main() {
$tests
}
''').create();
var test = await runTest([
'test.dart',
...args,
], reporter: 'github');
await test.shouldExit();
var stdoutLines = await test.stdoutStream().toList();
var actual = stdoutLines
.map((line) => line.trim())
.where((line) => line.isNotEmpty)
.join('\n');
// Un-indent the expected string.
var indentation = expected.indexOf(RegExp('[^ ]'));
expected = expected.split('\n').map((line) {
if (line.isEmpty) return line;
return line.substring(indentation);
}).join('\n');
expect(actual, useContains ? contains(expected) : equals(expected));
}