Skip to content

Commit 8eb474c

Browse files
authored
add a line to re-run the failed test after each failure in the compact reporter (#1736)
Closes #1734 Example output: <img width="906" alt="Screen Shot 2022-06-28 at 11 00 32 AM" src="https://user-images.githubusercontent.com/984921/176251403-63a99464-6de2-42ad-a85e-1c794d434c00.png">
1 parent fb4ccaf commit 8eb474c

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

pkgs/test/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Make the labels for test loading more readable in the compact and expanded
44
reporters, use gray instead of black.
5+
* Print a command to re-run the failed test after each failure in the compact
6+
reporter.
57
* Fix the package config path used when running pre-compiled vm tests.
68

79
## 1.21.3

pkgs/test/test/runner/compact_reporter_test.dart

+38
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
@TestOn('vm')
66

77
import 'dart:async';
8+
import 'dart:io';
89

10+
import 'package:path/path.dart' as p;
911
import 'package:test/test.dart';
1012
import 'package:test_descriptor/test_descriptor.dart' as d;
1113

@@ -426,6 +428,42 @@ void main() {
426428
For example, 'dart test --chain-stack-traces'.''',
427429
chainStackTraces: false);
428430
});
431+
432+
group('gives users a way to re-run failed tests', () {
433+
final executablePath = p.absolute(Platform.resolvedExecutable);
434+
435+
test('with simple names', () {
436+
return _expectReport('''
437+
test('failure', () {
438+
expect(1, equals(2));
439+
});''', '''
440+
+0: loading test.dart
441+
+0: failure
442+
+0 -1: failure [E]
443+
Expected: <2>
444+
Actual: <1>
445+
446+
To run this test again: $executablePath test test.dart -p vm --plain-name 'failure'
447+
448+
+0 -1: Some tests failed.''');
449+
});
450+
451+
test('escapes names containing single quotes', () {
452+
return _expectReport('''
453+
test("failure with a ' in the name", () {
454+
expect(1, equals(2));
455+
});''', '''
456+
+0: loading test.dart
457+
+0: failure with a ' in the name
458+
+0 -1: failure with a ' in the name [E]
459+
Expected: <2>
460+
Actual: <1>
461+
462+
To run this test again: $executablePath test test.dart -p vm --plain-name 'failure with a '\\'' in the name'
463+
464+
+0 -1: Some tests failed.''');
465+
});
466+
});
429467
}
430468

431469
Future<void> _expectReport(String tests, String expected,

pkgs/test_core/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Make the labels for test loading more readable in the compact and expanded
44
reporters, use gray instead of black.
5+
* Print a command to re-run the failed test after each failure in the compact
6+
reporter.
57
* Fix the package config path used when running pre-compiled vm tests.
68

79
## 0.4.15

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

+15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class CompactReporter implements Reporter {
4040
/// Windows or not outputting to a terminal.
4141
final String _gray;
4242

43+
/// The terminal escape code for cyan text, or the empty string if this is
44+
/// Windows or not outputting to a terminal.
45+
final String _cyan;
46+
4347
/// The terminal escape for bold text, or the empty string if this is
4448
/// Windows or not outputting to a terminal.
4549
final String _bold;
@@ -132,6 +136,7 @@ class CompactReporter implements Reporter {
132136
_red = color ? '\u001b[31m' : '',
133137
_yellow = color ? '\u001b[33m' : '',
134138
_gray = color ? '\u001b[90m' : '',
139+
_cyan = color ? '\u001b[36m' : '',
135140
_bold = color ? '\u001b[1m' : '',
136141
_noColor = color ? '\u001b[0m' : '' {
137142
_subscriptions.add(_engine.onTestStarted.listen(_onTestStarted));
@@ -214,6 +219,16 @@ class CompactReporter implements Reporter {
214219
if (message.type == MessageType.skip) text = ' $_yellow$text$_noColor';
215220
_sink.writeln(text);
216221
}));
222+
223+
liveTest.onComplete.then((_) {
224+
var result = liveTest.state.result;
225+
if (result != Result.error && result != Result.failure) return;
226+
_sink.writeln('');
227+
_sink.writeln('$_bold${_cyan}To run this test again:$_noColor '
228+
'${Platform.executable} test ${liveTest.suite.path} '
229+
'-p ${liveTest.suite.platform.runtime.identifier} '
230+
"--plain-name '${liveTest.test.name.replaceAll("'", r"'\''")}'");
231+
});
217232
}
218233

219234
/// A callback called when [liveTest]'s state becomes [state].

0 commit comments

Comments
 (0)