Skip to content

Commit

Permalink
feat: print test suite names in failure summary (#2624)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Aug 4, 2022
1 parent 0e3d6da commit 0dc266d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
46 changes: 27 additions & 19 deletions cli/src/cmd/forge/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl TestOutcome {

/// Iterator over all tests and their names
pub fn tests(&self) -> impl Iterator<Item = (&String, &TestResult)> {
self.results.values().flat_map(|SuiteResult { test_results, .. }| test_results.iter())
self.results.values().flat_map(|suite| suite.tests())
}

/// Returns an iterator over all `Test`
Expand All @@ -235,26 +235,34 @@ impl TestOutcome {

/// Checks if there are any failures and failures are disallowed
pub fn ensure_ok(&self) -> eyre::Result<()> {
if !self.allow_failure {
let failures = self.failures().count();
if failures > 0 {
println!();
println!("Failed tests:");
for (name, result) in self.failures() {
short_test_result(name, result);
}
println!();

let successes = self.successes().count();
println!(
"Encountered a total of {} failing tests, {} tests succeeded",
Paint::red(failures.to_string()),
Paint::green(successes.to_string())
);
std::process::exit(1);
let failures = self.failures().count();
if self.allow_failure || failures == 0 {
return Ok(())
}

println!();
println!("Failing tests:");
for (suite_name, suite) in self.results.iter() {
let failures = suite.failures().count();
if failures == 0 {
continue
}

let term = if failures > 1 { "tests" } else { "test" };
println!("Encountered {} failing {} in {}", failures, term, suite_name);
for (name, result) in suite.failures() {
short_test_result(name, result);
}
println!();
}
Ok(())

let successes = self.successes().count();
println!(
"Encountered a total of {} failing tests, {} tests succeeded",
Paint::red(failures.to_string()),
Paint::green(successes.to_string())
);
std::process::exit(1);
}

pub fn duration(&self) -> Duration {
Expand Down
19 changes: 18 additions & 1 deletion forge/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct SuiteResult {
pub duration: Duration,
/// Individual test results. `test method name -> TestResult`
pub test_results: BTreeMap<String, TestResult>,
// Warnings
/// Warnings
pub warnings: Vec<String>,
}

Expand All @@ -30,10 +30,27 @@ impl SuiteResult {
Self { duration, test_results, warnings }
}

/// Iterator over all succeeding tests and their names
pub fn successes(&self) -> impl Iterator<Item = (&String, &TestResult)> {
self.tests().filter(|(_, t)| t.success)
}

/// Iterator over all failing tests and their names
pub fn failures(&self) -> impl Iterator<Item = (&String, &TestResult)> {
self.tests().filter(|(_, t)| !t.success)
}

/// Iterator over all tests and their names
pub fn tests(&self) -> impl Iterator<Item = (&String, &TestResult)> {
self.test_results.iter()
}

/// Whether this test suite is empty.
pub fn is_empty(&self) -> bool {
self.test_results.is_empty()
}

/// The number of tests in this test suite.
pub fn len(&self) -> usize {
self.test_results.len()
}
Expand Down

0 comments on commit 0dc266d

Please sign in to comment.