Skip to content

Commit

Permalink
libtest: Print the names of failed tests eagerly
Browse files Browse the repository at this point in the history
Previously, libtest would wait until all tests finished running to print the progress, which made it
annoying to run many tests at once (since you don't know which have failed). Change it to print the
names as soon as they fail.
  • Loading branch information
jyn514 committed Nov 5, 2023
1 parent 33156aa commit 36dacde
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
24 changes: 17 additions & 7 deletions library/test/src/formatters/terse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ impl<T: Write> TerseFormatter<T> {
self.write_short_result(".", term::color::GREEN)
}

pub fn write_failed(&mut self) -> io::Result<()> {
self.write_short_result("F", term::color::RED)
pub fn write_failed(&mut self, name: &str) -> io::Result<()> {
// Put failed tests on their own line and include the test name, so that it's faster
// to see which test failed without having to wait for them all to run.
self.write_plain(format!("\n{name} --- "))?;
self.write_pretty("F", term::color::RED)?;
self.write_progress()
}

pub fn write_ignored(&mut self) -> io::Result<()> {
Expand All @@ -65,15 +69,21 @@ impl<T: Write> TerseFormatter<T> {
color: term::color::Color,
) -> io::Result<()> {
self.write_pretty(result, color)?;
if self.test_column % QUIET_MODE_MAX_COLUMN == QUIET_MODE_MAX_COLUMN - 1 {
self.test_column += 1;
if self.test_column == QUIET_MODE_MAX_COLUMN {
// We insert a new line regularly in order to flush the
// screen when dealing with line-buffered output (e.g., piping to
// `stamp` in the rust CI).
let out = format!(" {}/{}\n", self.test_column + 1, self.total_test_count);
self.write_plain(out)?;
self.write_progress()?;
}

self.test_column += 1;
Ok(())
}

fn write_progress(&mut self) -> io::Result<()> {
let out = format!(" {}/{}\n", self.test_column, self.total_test_count);
self.write_plain(out)?;
self.test_column = 0;
Ok(())
}

Expand Down Expand Up @@ -213,7 +223,7 @@ impl<T: Write> OutputFormatter for TerseFormatter<T> {
match *result {
TestResult::TrOk => self.write_ok(),
TestResult::TrFailed | TestResult::TrFailedMsg(_) | TestResult::TrTimedFail => {
self.write_failed()
self.write_failed(desc.name.as_slice())
}
TestResult::TrIgnored => self.write_ignored(),
TestResult::TrBench(ref bs) => {
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/test-attrs/terse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// compile-flags: --test
// run-fail
// run-flags: --test-threads=1 --quiet
// check-run-results
// exec-env:RUST_BACKTRACE=0
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
// ignore-emscripten no threads support
// needs-unwind

mod test {
#[test]
fn foo() {
panic!();
}

#[test]
fn bar() {}
#[test]
fn baz() {}
}
18 changes: 18 additions & 0 deletions tests/ui/test-attrs/terse.run.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

running 3 tests
..
test::foo --- F 2/3

failures:

---- test::foo stdout ----
thread 'test::foo' panicked at $DIR/terse.rs:13:9:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
test::foo

test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

0 comments on commit 36dacde

Please sign in to comment.