@@ -2998,3 +2998,141 @@ forgetest_init!(colored_traces, |prj, cmd| {
29982998 . assert_success( )
29992999 . stdout_eq( file![ "../fixtures/colored_traces.svg" : TermSvg ] ) ;
30003000} ) ;
3001+
3002+ // Tests that traces for successful tests can be suppressed by using `-s` flag.
3003+ // <https://github.com/foundry-rs/foundry/issues/9864>
3004+ forgetest_init ! ( should_only_show_failed_tests_trace, |prj, cmd| {
3005+ prj. add_test(
3006+ "SuppressTracesTest.t.sol" ,
3007+ r#"
3008+ import "forge-std/Test.sol";
3009+ import {Counter} from "../src/Counter.sol";
3010+
3011+ contract SuppressTracesTest is Test {
3012+ Counter public counter;
3013+
3014+ function setUp() public {
3015+ counter = new Counter();
3016+ counter.setNumber(0);
3017+ }
3018+
3019+ function test_increment_success() public {
3020+ console.log("test increment success");
3021+ counter.increment();
3022+ assertEq(counter.number(), 1);
3023+ }
3024+
3025+ function test_increment_failure() public {
3026+ console.log("test increment failure");
3027+ counter.increment();
3028+ assertEq(counter.number(), 100);
3029+ }
3030+ }
3031+ "# ,
3032+ )
3033+ . unwrap( ) ;
3034+
3035+ // Show traces and logs for failed test only.
3036+ cmd. args( [ "test" , "--mc" , "SuppressTracesTest" , "-vvvv" , "-s" ] ) . assert_failure( ) . stdout_eq(
3037+ str ![ [ r#"
3038+ [COMPILING_FILES] with [SOLC_VERSION]
3039+ [SOLC_VERSION] [ELAPSED]
3040+ Compiler run successful!
3041+
3042+ Ran 2 tests for test/SuppressTracesTest.t.sol:SuppressTracesTest
3043+ [FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3044+ Logs:
3045+ test increment failure
3046+
3047+ Traces:
3048+ [137242] SuppressTracesTest::setUp()
3049+ ├─ [96345] → new Counter@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
3050+ │ └─ ← [Return] 481 bytes of code
3051+ ├─ [2592] Counter::setNumber(0)
3052+ │ └─ ← [Stop]
3053+ └─ ← [Stop]
3054+
3055+ [35178] SuppressTracesTest::test_increment_failure()
3056+ ├─ [0] console::log("test increment failure") [staticcall]
3057+ │ └─ ← [Stop]
3058+ ├─ [22418] Counter::increment()
3059+ │ └─ ← [Stop]
3060+ ├─ [424] Counter::number() [staticcall]
3061+ │ └─ ← [Return] 1
3062+ ├─ [0] VM::assertEq(1, 100) [staticcall]
3063+ │ └─ ← [Revert] assertion failed: 1 != 100
3064+ └─ ← [Revert] assertion failed: 1 != 100
3065+
3066+ [PASS] test_increment_success() ([GAS])
3067+ Suite result: FAILED. 1 passed; 1 failed; 0 skipped; [ELAPSED]
3068+
3069+ Ran 1 test suite [ELAPSED]: 1 tests passed, 1 failed, 0 skipped (2 total tests)
3070+
3071+ Failing tests:
3072+ Encountered 1 failing test in test/SuppressTracesTest.t.sol:SuppressTracesTest
3073+ [FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3074+
3075+ Encountered a total of 1 failing tests, 1 tests succeeded
3076+
3077+ "# ] ] ,
3078+ ) ;
3079+
3080+ // Show traces and logs for all tests.
3081+ cmd. forge_fuse( )
3082+ . args( [ "test" , "--mc" , "SuppressTracesTest" , "-vvvv" ] )
3083+ . assert_failure( )
3084+ . stdout_eq( str ![ [ r#"
3085+ No files changed, compilation skipped
3086+
3087+ Ran 2 tests for test/SuppressTracesTest.t.sol:SuppressTracesTest
3088+ [FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3089+ Logs:
3090+ test increment failure
3091+
3092+ Traces:
3093+ [137242] SuppressTracesTest::setUp()
3094+ ├─ [96345] → new Counter@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
3095+ │ └─ ← [Return] 481 bytes of code
3096+ ├─ [2592] Counter::setNumber(0)
3097+ │ └─ ← [Stop]
3098+ └─ ← [Stop]
3099+
3100+ [35178] SuppressTracesTest::test_increment_failure()
3101+ ├─ [0] console::log("test increment failure") [staticcall]
3102+ │ └─ ← [Stop]
3103+ ├─ [22418] Counter::increment()
3104+ │ └─ ← [Stop]
3105+ ├─ [424] Counter::number() [staticcall]
3106+ │ └─ ← [Return] 1
3107+ ├─ [0] VM::assertEq(1, 100) [staticcall]
3108+ │ └─ ← [Revert] assertion failed: 1 != 100
3109+ └─ ← [Revert] assertion failed: 1 != 100
3110+
3111+ [PASS] test_increment_success() ([GAS])
3112+ Logs:
3113+ test increment success
3114+
3115+ Traces:
3116+ [35229] SuppressTracesTest::test_increment_success()
3117+ ├─ [0] console::log("test increment success") [staticcall]
3118+ │ └─ ← [Stop]
3119+ ├─ [22418] Counter::increment()
3120+ │ └─ ← [Stop]
3121+ ├─ [424] Counter::number() [staticcall]
3122+ │ └─ ← [Return] 1
3123+ ├─ [0] VM::assertEq(1, 1) [staticcall]
3124+ │ └─ ← [Return]
3125+ └─ ← [Stop]
3126+
3127+ Suite result: FAILED. 1 passed; 1 failed; 0 skipped; [ELAPSED]
3128+
3129+ Ran 1 test suite [ELAPSED]: 1 tests passed, 1 failed, 0 skipped (2 total tests)
3130+
3131+ Failing tests:
3132+ Encountered 1 failing test in test/SuppressTracesTest.t.sol:SuppressTracesTest
3133+ [FAIL: assertion failed: 1 != 100] test_increment_failure() ([GAS])
3134+
3135+ Encountered a total of 1 failing tests, 1 tests succeeded
3136+
3137+ "# ] ] ) ;
3138+ } ) ;
0 commit comments