Skip to content

♻️ Add --summary flag to print test summary table - #5961

Merged
mattsse merged 14 commits into
foundry-rs:masterfrom
PraneshASP:feat/test-summary
Oct 8, 2023
Merged

mattsse merged 14 commits into
foundry-rs:masterfrom
PraneshASP:feat/test-summary

Conversation

@PraneshASP

Copy link
Copy Markdown
Contributor

Motivation

For projects with large number of testcases, the current output format is not friendly enough to quickly glance through the test suites. Scrolling through the output in the terminal is not quite user-friendly especially if the output is huge. It would be nice to have the test command to accept a flag and print the tests summary in a tabular format.

Solution

This pull request introduces a --summary flag for the test command. When this flag is passed, the test runner will output a summary of test results in a neatly formatted table. This tabular format will include columns for the test suite name, number of tests passed, failed, and skipped.

Example:

forge test --summary

Sample Output (Seaport codebase):

Screenshot 2023-10-01 at 1 15 23 AM

This can be further developed into something as sophisticated as hardhat-gas-report table in the future if required.

@Evalir Evalir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey! thanks for the contribution. Supportive—albeit I'd like some discussion around this first cc @mattsse @mds1 i think we had talked about this exact thing a week earlier

some nits in the meanwhile!

Comment thread crates/forge/bin/cmd/test/mod.rs Resolved
Comment thread crates/forge/bin/cmd/test/mod.rs Resolved
format_aggregated_summary(num_test_suites, total_passed, total_failed, total_skipped)
);

if summary {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering, can we move all the work for building and displaying the table to this if as it's only necessary if it's actually going to be displayed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require storing all the test suite name and results to a separate object inside the loop and then later read inside the if block? I wanted to avoid adding new variable/storage.

We have to do the same to display the contract names in sorted order as well I guess. What do you guys think? Do you suggest storing the test results? @mds1 @Evalir @mattsse

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I think in the end if we have to sort the contracts we have to do it—so feel free to go ahead!

@mattsse mattsse left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a reasonable addition, and because this is opt-in, I don't see why we shouldn't include it.

pending nits

also I think we should sort the contracts by name

Comment thread crates/forge/bin/cmd/test/mod.rs Outdated
Comment on lines +628 to +631
Cell::new("Failed")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold)
.fg(Color::Red),

@mds1 mds1 Oct 2, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we already showed red, but I'd avoid showing any red unless we actually have a failure. If there is a failure, the header name can be red, and the cells where there were failures, but the rest should be the default color

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Should we do the same for all 3 columns or only for failures?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm good question. What do other frameworks do? I don't want to overcomplicate it, mainly just don't like that we currently always show red—it looks bad at a glance since everyone associates red=bad

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo, I think we could go for something simple: Unless the number is 0, show the corresponding color. I htink this works well because:

  • If it's 0, no need to look at it.
  • Ideally all your passed suites should look green :)
  • If you have failed or skipped tests, you probably want something to grab your attention so you can double check or at least acknowledge it.

Comment thread crates/forge/bin/cmd/test/mod.rs Outdated
fn create_test_summary_table_header(summary_table: &mut Table) -> Row {
summary_table.apply_modifier(UTF8_ROUND_CORNERS);
Row::from(vec![
Cell::new("Test Suites")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a verbose version of the summary that splits this into two "File Path" and "Contract Name" columns, since users may have the same contract name in multiple files, so that helps disambiguate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean adding another flag like --summary-detailed? Can also include the test file path by default if it makes sense.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd maybe do this as a --detailed flag which requires --summary to be enabled

Comment thread crates/forge/bin/cmd/test/mod.rs Outdated

if summary {
let row = create_test_summary_table_row(
contract_name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like @mattsse said, alphabetical sorting here would be nice, it's unclear what the current ordering is

@mds1

mds1 commented Oct 2, 2023

Copy link
Copy Markdown
Contributor

This is nice overall, thanks @PraneshASP! Just left a few nits

@PraneshASP
PraneshASP requested a review from Evalir October 2, 2023 17:29

@Evalir Evalir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good! this is only pending the few nits the others have left.

@Evalir

Evalir commented Oct 6, 2023

Copy link
Copy Markdown
Contributor

friendly ping @PraneshASP

@PraneshASP

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions! Will work on the refactor over the weekend.

@PraneshASP

PraneshASP commented Oct 7, 2023 •

Copy link
Copy Markdown
Contributor Author

Made all the changes as suggested.

  • Added --detailed param with error handling to print file path and duration of the test suite (see screenshot)
  • Results are alphabetically sorted now (based on Test Suite name)
  • Cells are colored only if the count is > 0

Screenshots for reference:

Command:

forge test --summary

Output:

Screenshot 2023-10-08 at 1 00 08 AM

Command:

forge test --summary -detailed

Output

Screenshot 2023-10-08 at 1 00 22 AM

@PraneshASP
PraneshASP requested review from Evalir, mattsse and mds1 October 7, 2023 19:37

@mattsse mattsse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice,

I moved some things around

@mattsse
mattsse merged commit fb51f2a into foundry-rs:master Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants