5/5
## Fuzz Tests Setup Now that we have written a lot of unit tests, we are going to write some fuzz tests. Let's create a new folder called `fuzz`. We will create a test file called `test_fuzz.py`. ```python from hypothesis.stateful import RuleBasedStateMachine, initialize, rule from script.deploy_dsc_engine import deploy_dsc_engine from moccasin.config import get_active_network from eth_constants import ZERO_ADDRESS from boa.util.abi import Address USERS_SIZE = 10 class StablecoinFuzzer(RuleBasedStateMachine): def __init__(self): super().__init__() self.dsc = deploy_dsc() self.dsc = deploy_dsc_engine(self.dsc) self.active_network = get_active_network() self.weth = self.active_network.manifest(named="weth") self.wbtc = self.active_network.manifest(named="wbtc") self.eth_usd = self.active_network.manifest(named="eth_usd_price_feed") self.btc_usd = self.active_network.manifest(named="btc_usd_price_feed") self.users = [Address("0x" + ZERO_ADDRESS.hex())] while Address("0x" + ZERO_ADDRESS.hex()) in self.users: self.users = [Address("0x" + ZERO_ADDRESS.hex()) for _ in range(USERS_SIZE)] stablecoin_fuzzer = StablecoinFuzzer.TestCase ``` We are going to import the `rule` from the `hypothesis.stateful` module. We will initialize our test suite here using the `initialize` function, and we need to import the `deploy_dsc_engine` from the `deploy_dsc_engine` module. We will set up all of our contracts. ```python from hypothesis.stateful import RuleBasedStateMachine, initialize, rule from script.deploy_dsc_engine import deploy_dsc_engine from moccasin.config import get_active_network from eth_constants import ZERO_ADDRESS from boa.util.abi import Address USERS_SIZE = 10 class StablecoinFuzzer(RuleBasedStateMachine): def __init__(self): super().__init__() self.dsc = deploy_dsc() self.dsc = deploy_dsc_engine(self.dsc) self.active_network = get_active_network() self.weth = self.active_network.manifest(named="weth") self.wbtc = self.active_network.manifest(named="wbtc") self.eth_usd = self.active_network.manifest(named="eth_usd_price_feed") self.btc_usd = self.active_network.manifest(named="btc_usd_price_feed") self.users = [Address("0x" + ZERO_ADDRESS.hex())] while Address("0x" + ZERO_ADDRESS.hex()) in self.users: self.users = [Address("0x" + ZERO_ADDRESS.hex()) for _ in range(USERS_SIZE)] stablecoin_fuzzer = StablecoinFuzzer.TestCase ``` We are going to create an array of users as well. We will create an array of ten different users, and we can even print the array. ```python from hypothesis.stateful import RuleBasedStateMachine, initialize, rule from script.deploy_dsc_engine import deploy_dsc_engine from moccasin.config import get_active_network from eth_constants import ZERO_ADDRESS from boa.util.abi import Address USERS_SIZE = 10 class StablecoinFuzzer(RuleBasedStateMachine): def __init__(self): super().__init__() self.dsc = deploy_dsc() self.dsc = deploy_dsc_engine(self.dsc) self.active_network = get_active_network() self.weth = self.active_network.manifest(named="weth") self.wbtc = self.active_network.manifest(named="wbtc") self.eth_usd = self.active_network.manifest(named="eth_usd_price_feed") self.btc_usd = self.active_network.manifest(named="btc_usd_price_feed") self.users = [Address("0x" + ZERO_ADDRESS.hex())] while Address("0x" + ZERO_ADDRESS.hex()) in self.users: self.users = [Address("0x" + ZERO_ADDRESS.hex()) for _ in range(USERS_SIZE)] stablecoin_fuzzer = StablecoinFuzzer.TestCase ``` We will import the Boa module and create a rule for our protocol. ```python from hypothesis.stateful import RuleBasedStateMachine, initialize, rule from script.deploy_dsc_engine import deploy_dsc_engine from moccasin.config import get_active_network from eth_constants import ZERO_ADDRESS from boa.util.abi import Address USERS_SIZE = 10 class StablecoinFuzzer(RuleBasedStateMachine): def __init__(self): super().__init__() self.dsc = deploy_dsc() self.dsc = deploy_dsc_engine(self.dsc) self.active_network = get_active_network() self.weth = self.active_network.manifest(named="weth") self.wbtc = self.active_network.manifest(named="wbtc") self.eth_usd = self.active_network.manifest(named="eth_usd_price_feed") self.btc_usd = self.active_network.manifest(named="btc_usd_price_feed") self.users = [Address("0x" + ZERO_ADDRESS.hex())] while Address("0x" + ZERO_ADDRESS.hex()) in self.users: self.users = [Address("0x" + ZERO_ADDRESS.hex()) for _ in range(USERS_SIZE)] stablecoin_fuzzer = StablecoinFuzzer.TestCase @rule def pass_me(): pass ``` Now, we can initialize our test suite and see it print out ten different addresses. ```bash mox test -s -k stablecoin_fuzzer ```
A comprehensive guide to setting up fuzz tests in a Solidity smart contract. The lesson covers the basics of setting up a fuzz testing environment, writing the fuzz test, initializing the testing suite, and defining rules for the fuzz test.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 10, 2025
Duration: 2h 21min
Duration: 1h 58min
Duration: 2h 55min
Duration: 1h 55min
Duration: 46min
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 10, 2025