foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Fuzz testing only catches console.log in `setUp()` and in failing test functions, not passing functions

Open webthethird opened this issue 2 years ago • 16 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (2e99f7f 2022-12-03T00:08:28.619060629Z)

What command(s) is the bug in?

forge test

Operating System

Linux

Describe the bug

Similar to issue #655, which was closed as stale (creating a new issue since there have been changes since that issue, and mine may be subtly different).

Only console logs in failing functions and setUp() are showing in the Logs section of the results, above the traces, but not the logs in passing functions. I can see the other calls to console::log in the traces, but they're not getting formatted.

Running 1 test for src/test/compound/Comptroller-fork-diff.sol:TestComptroller
[PASS] test_claimComp_diff_before_after(uint8) (runs: 1, μ: 2924860, ~: 2924860)
Logs:
  Number of addresses: 74

Traces:
  [4852548] TestComptroller::setUp() 
    ├─ [0] console::log(Number of addresses: %s, 74) [staticcall]
    │   └─ ← ()
...
  [2924860] TestComptroller::test_claimComp_diff_before_after(5) 
    ├─ [0] console::log(Address %s, 0x000008599eB841e58b3e13305Da3dA5df5Ed8fFD) [staticcall]
    │   └─ ← ()

This would be fine if I were only trying to do one run, but since I'm fuzzing, I'd like to see all of the logs without printing all of the traces.

I have added a minimized test contract in my repo for reproducing this bug: https://github.com/webthethird/solidity-diff-fuzz-upgrades/blob/main/src/test/test_logging.sol

webthethird avatar Dec 06 '22 20:12 webthethird

Would be great to fix this. Any updates?

tommyrharper avatar Jan 21 '23 17:01 tommyrharper

Simple way to reproduce:

import "forge-std/Test.sol";

contract Test5 is Test {
  function setUp() public {
    console2.log("Test5.setUp()");
  }
  function test_FuzzVals(uint x) external {
    console2.log("Test5.test_FuzzVals()");
  }
}

As shown in the screenshot below, forge test with any verbosity shows the Test5.setUp() log, but never shows the Test5.test_FuzzVals() log. However, in the trace, both are shown.

image

When all runs of a fuzz test are successful, the trace shows the values from the last run. The console.logs should do the same

mds1 avatar Jan 24 '23 17:01 mds1

running into the same issue. tried both console and console2 but its not outputting . only prints if it is in setUp()

upalchowdhury avatar Jun 24 '23 17:06 upalchowdhury

cc @Evalir, I just verified this bug is still occurring on the latest nightly using the repro in https://github.com/foundry-rs/foundry/issues/3843#issuecomment-1402311583. Guessing this is a simple fix

mds1 avatar Jun 24 '23 20:06 mds1

So, I can confirm this is an issue—even though just fixing it could make everyone's CI or local runs extremely noisy as a default. @mds1 this is somewhat easy fix, but I wonder if we should put this behind some kind of flag or increase the verbosity level needed for printing fuzz logs?

Evalir avatar Jun 29 '23 03:06 Evalir

even though just fixing it could make everyone's CI or local runs extremely noisy as a default

We already would show the logs if it was a regular test and not a fuzz test, so I think this is ok? Unclear why we'd want to handle the console.logs differently between the two, it seems unintuitive. To hide the logs you'd just run with -v as the verbosity

mds1 avatar Jun 29 '23 14:06 mds1

Any update on this issue? I really need to show logs in fuzz tests for both failed and success results.

danielpham765 avatar Aug 31 '23 20:08 danielpham765

Bump to this, also looking to get output on each fuzz test, not just the failing ones

brucdarc avatar Sep 22 '23 19:09 brucdarc

+1 to this use case being valuable

nicovalencia avatar Nov 09 '23 16:11 nicovalencia

still have it ;(

d3mage avatar Nov 14 '23 14:11 d3mage

+1 to this is

sujithsomraaj avatar Mar 12 '24 16:03 sujithsomraaj

+1 Let me know if you need any help with adding the fix.

deepdfs avatar Mar 20 '24 12:03 deepdfs

  • 1 to this

yenchihliao avatar Mar 25 '24 02:03 yenchihliao

having this feature would be great.

I think the only readability issue will be there because fuzzing runs the function too many times and logging every time will spam the terminal

Zartaj0 avatar Apr 04 '24 07:04 Zartaj0

+1 to this

0xNilesh avatar Apr 08 '24 09:04 0xNilesh

+1 to this

This feature is immensely required.

m-waqas88 avatar Apr 22 '24 20:04 m-waqas88

https://github.com/foundry-rs/foundry/pull/7781 is now merged

grandizzy avatar May 15 '24 09:05 grandizzy

exciting...

Azleal avatar May 16 '24 07:05 Azleal

#7781 is reverted in #7935 Should this issue be reopened?

louislung avatar Jun 27 '24 10:06 louislung

I think we could track it woth https://github.com/foundry-rs/foundry/issues/3844

grandizzy avatar Jun 27 '24 10:06 grandizzy

I think we could track it woth https://github.com/foundry-rs/foundry/issues/3844

how should this feature be implemented? i didn't take every situation into consideration in my last try. i'm still using locally built foundry when fuzz logs are needed 😅

Azleal avatar Jun 28 '24 10:06 Azleal

+1 would love to see fuzzing results logged

wagmi33 avatar Jul 02 '24 11:07 wagmi33

+1. when will this be available ?

novaknole avatar Jul 04 '24 21:07 novaknole

+1

0xJuancito avatar Aug 21 '24 00:08 0xJuancito

+1

It's already implemented, see here for fuzz config and here for fuzz inline config

Azleal avatar Aug 21 '24 01:08 Azleal