Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: print test suite names in failure summary #2624

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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