Skip to content

Commit 52c5c9c

Browse files
committed
Add -s/--summary flag to print failed tests at end of run
1 parent db0c811 commit 52c5c9c

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

pkgs/test/test/runner/compact_reporter_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,37 @@ void main() {
562562
);
563563
});
564564
}, testOn: 'windows');
565+
566+
test('prints failure summary with --summary flag', () {
567+
return _expectReport(
568+
'''
569+
test('failure 1', () => throw TestFailure('oh no'));
570+
test('success 1', () {});
571+
test('failure 2', () => throw TestFailure('oh no'));''',
572+
'''
573+
+0: loading test.dart
574+
+0: failure 1
575+
+0 -1: failure 1 [E]
576+
oh no
577+
test.dart 6:33 main.<fn>
578+
579+
580+
+0 -1: success 1
581+
+1 -1: success 1
582+
+1 -1: failure 2
583+
+1 -2: failure 2 [E]
584+
oh no
585+
test.dart 8:33 main.<fn>
586+
587+
588+
+1 -2: Some tests failed.
589+
590+
Failing tests:
591+
test.dart: failure 1
592+
test.dart: failure 2''',
593+
args: ['--summary'],
594+
);
595+
});
565596
}
566597

567598
Future<void> _expectReport(

pkgs/test_core/lib/src/runner/configuration.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ class Configuration {
104104
bool get color => _color ?? canUseSpecialChars;
105105
final bool? _color;
106106

107+
/// Whether to print a summary of failed tests at the end.
108+
bool get printSummary => _printSummary ?? false;
109+
final bool? _printSummary;
110+
107111
/// How many tests to run concurrently.
108112
int get concurrency =>
109113
pauseAfterLoad ? 1 : (_concurrency ?? defaultConcurrency);
@@ -274,6 +278,7 @@ class Configuration {
274278
required bool? pauseAfterLoad,
275279
required bool? debug,
276280
required bool? color,
281+
required bool? printSummary,
277282
required String? configurationPath,
278283
required String? reporter,
279284
required Map<String, String>? fileReporters,
@@ -330,6 +335,7 @@ class Configuration {
330335
pauseAfterLoad: pauseAfterLoad,
331336
debug: debug,
332337
color: color,
338+
printSummary: printSummary,
333339
configurationPath: configurationPath,
334340
reporter: reporter,
335341
fileReporters: fileReporters,
@@ -392,6 +398,7 @@ class Configuration {
392398
bool? pauseAfterLoad,
393399
bool? debug,
394400
bool? color,
401+
bool? printSummary,
395402
String? configurationPath,
396403
String? reporter,
397404
Map<String, String>? fileReporters,
@@ -446,6 +453,7 @@ class Configuration {
446453
pauseAfterLoad: pauseAfterLoad,
447454
debug: debug,
448455
color: color,
456+
printSummary: printSummary,
449457
configurationPath: configurationPath,
450458
reporter: reporter,
451459
fileReporters: fileReporters,
@@ -517,6 +525,7 @@ class Configuration {
517525
pauseAfterLoad: null,
518526
debug: null,
519527
color: null,
528+
printSummary: null,
520529
configurationPath: null,
521530
reporter: null,
522531
fileReporters: null,
@@ -584,6 +593,7 @@ class Configuration {
584593
pauseAfterLoad: null,
585594
debug: null,
586595
color: null,
596+
printSummary: null,
587597
configurationPath: null,
588598
reporter: null,
589599
fileReporters: null,
@@ -656,6 +666,7 @@ class Configuration {
656666
version: null,
657667
debug: null,
658668
color: null,
669+
printSummary: null,
659670
configurationPath: null,
660671
coverage: null,
661672
coverageLcov: null,
@@ -718,6 +729,7 @@ class Configuration {
718729
pauseAfterLoad: null,
719730
debug: null,
720731
color: null,
732+
printSummary: null,
721733
configurationPath: null,
722734
reporter: null,
723735
fileReporters: null,
@@ -788,6 +800,7 @@ class Configuration {
788800
required bool? pauseAfterLoad,
789801
required bool? debug,
790802
required bool? color,
803+
required bool? printSummary,
791804
required String? configurationPath,
792805
required String? reporter,
793806
required Map<String, String>? fileReporters,
@@ -818,6 +831,7 @@ class Configuration {
818831
_pauseAfterLoad = pauseAfterLoad,
819832
_debug = debug,
820833
_color = color,
834+
_printSummary = printSummary,
821835
_configurationPath = configurationPath,
822836
_reporter = reporter,
823837
fileReporters = fileReporters ?? {},
@@ -883,6 +897,7 @@ class Configuration {
883897
pauseAfterLoad: null,
884898
debug: null,
885899
color: null,
900+
printSummary: null,
886901
configurationPath: null,
887902
reporter: null,
888903
fileReporters: null,
@@ -986,6 +1001,7 @@ class Configuration {
9861001
pauseAfterLoad: other._pauseAfterLoad ?? _pauseAfterLoad,
9871002
debug: other._debug ?? _debug,
9881003
color: other._color ?? _color,
1004+
printSummary: other._printSummary ?? _printSummary,
9891005
configurationPath: other._configurationPath ?? _configurationPath,
9901006
reporter: other._reporter ?? _reporter,
9911007
fileReporters: mergeMaps(fileReporters, other.fileReporters),
@@ -1044,6 +1060,7 @@ class Configuration {
10441060
bool? pauseAfterLoad,
10451061
bool? debug,
10461062
bool? color,
1063+
bool? printSummary,
10471064
String? configurationPath,
10481065
String? reporter,
10491066
Map<String, String>? fileReporters,
@@ -1095,6 +1112,7 @@ class Configuration {
10951112
pauseAfterLoad: pauseAfterLoad ?? _pauseAfterLoad,
10961113
debug: debug ?? _debug,
10971114
color: color ?? _color,
1115+
printSummary: printSummary ?? _printSummary,
10981116
configurationPath: configurationPath ?? _configurationPath,
10991117
reporter: reporter ?? _reporter,
11001118
fileReporters: fileReporters ?? this.fileReporters,

pkgs/test_core/lib/src/runner/configuration/args.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ final ArgParser _parser =
262262
'color',
263263
help: 'Use terminal colors.\n(auto-detected by default)',
264264
);
265+
parser.addFlag(
266+
'summary',
267+
abbr: 's',
268+
help: 'Print a summary of all failed tests at the end of the run.',
269+
defaultsTo: false,
270+
);
265271

266272
/// The following options are used only by the internal Google test runner.
267273
/// They're hidden and not supported as stable API surface outside Google.
@@ -460,6 +466,7 @@ class _Parser {
460466
verboseTrace: _ifParsed('verbose-trace'),
461467
chainStackTraces: _ifParsed('chain-stack-traces'),
462468
jsTrace: _ifParsed('js-trace'),
469+
printSummary: _ifParsed('summary'),
463470
pauseAfterLoad: _ifParsed('pause-after-load'),
464471
debug: _ifParsed('debug'),
465472
color: color,

pkgs/test_core/lib/src/runner/configuration/reporters.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final _allReporters = <String, ReporterDetails>{
4343
printPlatform:
4444
config.suiteDefaults.runtimes.length > 1 ||
4545
config.suiteDefaults.compilerSelections != null,
46+
printSummary: config.printSummary,
4647
),
4748
),
4849
'expanded': ReporterDetails(

pkgs/test_core/lib/src/runner/reporter/compact.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class CompactReporter implements Reporter {
6161
/// Whether the platform each test is running on should be printed.
6262
final bool _printPlatform;
6363

64+
/// Whether to print a summary of failed tests at the end.
65+
final bool _printSummary;
66+
6467
/// A stopwatch that tracks the duration of the full run.
6568
final _stopwatch = Stopwatch();
6669

@@ -124,12 +127,14 @@ class CompactReporter implements Reporter {
124127
required bool color,
125128
required bool printPath,
126129
required bool printPlatform,
130+
required bool printSummary,
127131
}) => CompactReporter._(
128132
engine,
129133
sink,
130134
color: color,
131135
printPath: printPath,
132136
printPlatform: printPlatform,
137+
printSummary: printSummary,
133138
);
134139

135140
CompactReporter._(
@@ -138,8 +143,10 @@ class CompactReporter implements Reporter {
138143
required bool color,
139144
required bool printPath,
140145
required bool printPlatform,
146+
required bool printSummary,
141147
}) : _printPath = printPath,
142148
_printPlatform = printPlatform,
149+
_printSummary = printSummary,
143150
_color = color,
144151
_green = color ? '\u001b[32m' : '',
145152
_red = color ? '\u001b[31m' : '',
@@ -342,6 +349,20 @@ class CompactReporter implements Reporter {
342349
}
343350
_progressLine('Some tests failed.', color: _red);
344351
_sink.writeln('');
352+
353+
if (_printSummary && _engine.failed.isNotEmpty) {
354+
_sink.writeln('');
355+
_sink.writeln('${_red}Failing tests:$_noColor');
356+
for (final liveTest in _engine.failed) {
357+
final path = liveTest.suite.path;
358+
final name = liveTest.test.name;
359+
if (path != null) {
360+
_sink.writeln(' $path: $name');
361+
} else {
362+
_sink.writeln(' $name');
363+
}
364+
}
365+
}
345366
} else if (_engine.passed.isEmpty) {
346367
_progressLine('${_yellow}All tests skipped.$_noColor');
347368
_sink.writeln('');

0 commit comments

Comments
 (0)