Skip to main content
Fuzz testing is a technique for testing general robustness of code by running many tests with random inputs. This is useful for assertions as it helps identify edge cases and unexpected behavior. You can use Forge’s built-in fuzz testing capabilities to test your assertions with random inputs.

Example

This would run the test with 256 different values for withdrawalAmount that adhere to the bounds set by the vm.assume() constraints.
Use vm.assume() to filter out invalid inputs that would cause the test to fail for reasons unrelated to your assertion logic.

Best Practices

  1. Set reasonable bounds - Use vm.assume() to constrain inputs to valid ranges
  2. Test multiple parameters - Fuzz multiple inputs simultaneously to catch interaction bugs
  3. Use meaningful ranges - Consider the actual values your protocol will encounter
  4. Check valid edge cases - Ensure assertions pass for valid edge cases

Running Fuzz Tests

Fuzz tests run automatically with pcl test:
You can configure the number of runs in your foundry.toml:
Consider creating a separate profile for fuzz tests so they don’t run during quick local development cycles. See CI/CD Integration for complete profile configuration examples.
Learn More: