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(fuzz): add option to use only defined fixtures #9638

Open
2 tasks done
Dzejkop opened this issue Jan 7, 2025 · 2 comments
Open
2 tasks done

feat(fuzz): add option to use only defined fixtures #9638

Dzejkop opened this issue Jan 7, 2025 · 2 comments
Labels
Cmd-forge-test Command: forge test T-feature Type: feature

Comments

@Dzejkop
Copy link

Dzejkop commented Jan 7, 2025

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.3.0 (5a8bd89 2024-12-20T08:45:53.204298000Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

Minimal example repo

Docs suggest we can setup fixtures for testing with specific values using storage arrays or functions named like fixtureParamName. This doesn't work.

Code from the repo above:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test} from "forge-std/Test.sol";

contract FixtureTest is Test {
    function fixtureA() public returns (uint256[] memory) {
      uint256[] memory nums = new uint256[](1);
      nums[0] = 1;
      return nums;
    }

    function fixture_a() public returns (uint256[] memory) {
      uint256[] memory nums = new uint256[](1);
      nums[0] = 1;
      return nums;
    }

    uint256[] fixtureB = [1];
    uint256[] fixture_b = [1];

    function testFuzz_DoesNotWorkFun(uint256 a) public {
      assertEq(a, 1);
    }

    function testFuzz_DoesNotWorkStorage(uint256 b) public {
      assertEq(b, 1);
    }
}

Example test output:

Ran 2 tests for test/Fixture.t.sol:FixtureTest
[FAIL: assertion failed: 3 != 1; counterexample: calldata=0x0a5b9e0a0000000000000000000000000000000000000000000000000000000000000003 args=[3]] testFuzz_DoesNotWorkFun(uint256) (runs: 1, μ: 3[17](https://github.com/Dzejkop/forge-test-fuzz-not-working-minimal-example/actions/runs/12651348861/job/35251762954#step:7:18)2, ~: 3172)
Traces:
  [3164] FixtureTest::testFuzz_DoesNotWorkFun(3)
    ├─ [0] VM::assertEq(3, 1) [staticcall]
    │   └─ ← [Revert] assertion failed: 3 != 1
    └─ ← [Revert] assertion failed: 3 != 1

[FAIL: assertion failed: 36[18](https://github.com/Dzejkop/forge-test-fuzz-not-working-minimal-example/actions/runs/12651348861/job/35251762954#step:7:19) != 1; counterexample: calldata=0x90e665580000000000000000000000000000000000000000000000000000000000000e22 args=[3618]] testFuzz_DoesNotWorkStorage(uint256) (runs: 0, μ: 0, ~: 0)
Traces:
  [3141] FixtureTest::testFuzz_DoesNotWorkStorage(3618)
    ├─ [0] VM::assertEq(3618, 1) [staticcall]
    │   └─ ← [Revert] assertion failed: 3618 != 1
    └─ ← [Revert] assertion failed: 3618 != 1
@Dzejkop Dzejkop added T-bug Type: bug T-needs-triage Type: this issue needs to be labelled labels Jan 7, 2025
@github-project-automation github-project-automation bot moved this to Todo in Foundry Jan 7, 2025
@grandizzy
Copy link
Collaborator

@Dzejkop per https://book.getfoundry.sh/forge/fuzz-testing#fuzz-test-fixtures

Fuzz test fixtures can be defined when you want to make sure a certain set of values is used as inputs for fuzzed parameters.

which means that is guaranteed that fixture values will be used as fuzz inputs, along other inputs (see https://book.getfoundry.sh/images/fuzzer.png fixtures represents 40% from fuzzed inputs)

We could extend this and have an option to use only fixtures when fuzzing, lmk your thoughts. thank you!

@Dzejkop
Copy link
Author

Dzejkop commented Jan 7, 2025

I see, makes sense!

Maybe it would make sense then to split this into fuzzing and prop testing. Perhaps explicit testFuzz and testProp prefixes?

@grandizzy grandizzy added T-feature Type: feature Cmd-forge-test Command: forge test and removed T-bug Type: bug T-needs-triage Type: this issue needs to be labelled labels Jan 7, 2025
@grandizzy grandizzy changed the title Fuzz testing fixtures don't work as advertised feat(fuzz): add option to use only defined fixtures Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cmd-forge-test Command: forge test T-feature Type: feature
Projects
Status: Todo
Development

No branches or pull requests

2 participants