Fixtures, Time Control, Coverage and Fuzzing
Larger test suites need speed, isolation and tools that reveal what is untested. This lesson explains fixtures and snapshots, manipulating time and accounts on the local network, measuring coverage and writing fuzz tests.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how loadFixture uses snapshots.
- Describe the network helpers that control time and accounts.
- Interpret what coverage does and does not show.
- Explain how fuzz tests generate inputs.
Fixtures and snapshots
Deploying contracts before every test is slow and repetitive. A fixture is a function that builds a known starting state. Hardhat's loadFixture works as the following sequence shows.
-
First call
Runs the fixture: deploys contracts, mints tokens, grants roles.
-
Snapshot
Takes a snapshot of the whole chain state after the fixture.
-
Later calls
Reverts the chain to the snapshot instead of running the fixture again.
Each test therefore starts from an identical state, regardless of what earlier tests did, and the suite runs much faster. Fixtures must be named functions, not anonymous functions defined inside each test, so that loadFixture recognises them.
Controlling time and accounts
Contracts often depend on time, and tests must not wait for real time to pass. Hardhat's network helpers control the local chain, as the following table shows.
| Helper | Effect |
|---|---|
networkHelpers.time.increase(seconds) | Moves the clock forward and mines a block |
networkHelpers.time.increaseTo(timestamp) | Mines a block with exactly this timestamp |
networkHelpers.time.latest() | Returns the latest block's timestamp |
networkHelpers.mine(n) | Mines n blocks |
networkHelpers.impersonateAccount(addr) | Sends transactions as any address, useful on forks |
networkHelpers.setBalance(addr, wei) | Sets an account's ether balance |
it("allows refunds only after the deadline", async function () {
const { fund, alice } = await networkHelpers.loadFixture(deployFund);
await expect(fund.connect(alice).refund()).to.be.revertedWithCustomError(fund, "TooEarly");
await networkHelpers.time.increase(30 * 24 * 60 * 60); // 30 days
await expect(fund.connect(alice).refund()).not.to.revert(ethers);
});Foundry and Hardhat Solidity tests use the cheatcodes vm.warp(t), vm.roll(n), vm.prank(addr) and vm.deal(addr, amount) for the same purposes.
Network helpers and cheatcodes
Coverage
The following command runs the tests with instrumentation and reports, per file, the percentage of statements, branches, functions and lines executed.
npx hardhat test --coverageCoverage shows what the tests did not execute, such as an untested else branch or error path. It cannot show that assertions are correct or complete: a test that calls every function but asserts nothing produces high coverage and finds no bugs.
What coverage tells you
Fuzz tests
A fuzz test is a Solidity test function with parameters. The runner calls it many times with generated values, deliberately including edge cases such as 0, 1 and the type's maximum, and reports the smallest failing input it finds.
function testFuzz_WithdrawNeverExceedsDeposit(uint96 deposit, uint96 withdrawal) public {
bank.deposit{value: deposit}();
if (withdrawal > deposit) {
vm.expectRevert();
}
bank.withdraw(withdrawal);
}Exam tip
Choose parameter types that bound inputs to realistic ranges (such as uint96 for ether amounts), or filter with vm.assume, so that the fuzzer spends its runs on meaningful cases.
Fuzz tests
Tests that interfere
A test suite passes when tests run individually but fails when they run together: a test expecting a zero balance fails because an earlier test deposited ether. How should the suite be fixed?
The tests share state. Move contract deployment into a named fixture and start every test with await networkHelpers.loadFixture(deployBank). Each test then begins from the same snapshot, so the order of tests no longer matters.
Testing vesting
A vesting contract releases 25% of tokens after one year and the rest monthly over the next three years. How should its tests handle time?
Use time.increaseTo to jump to exact moments: just before the one-year cliff (expect nothing releasable), exactly at it (expect 25%), one month later (expect 25% plus one 36th of the remaining 75%), and after four years (expect 100%). Testing just before and after each boundary catches off-by-one errors in the time arithmetic.
What does loadFixture do on its second and later calls with the same fixture?
Show answer
It restores the chain to the snapshot taken after the first run instead of running the fixture again, so each test starts from the same state quickly.
Exam Tips
Remember for the exam
- loadFixture runs a named fixture once, snapshots, and restores the snapshot for each test.
- Use time helpers (time.increase, increaseTo) or vm.warp instead of real waiting.
- npx hardhat test --coverage shows untested code, not correct assertions.
- Fuzz tests are Solidity test functions with parameters; bound their input types.