Forge Test Explained in Detail
A Forge test is a Solidity contract that tests another Solidity contract. Foundry runs these tests with forge test and provides cheatcodes for changing caller, time, block number, storage, and expected reverts.
Forge tests are common in audits because exploit PoCs can be written close to the code being tested.
Smart contract example
function testWithdrawRevertsForZeroBalance() public {
vm.expectRevert();
vault.withdraw();
}
This test expects the withdrawal to fail.
Forge Test in Auditing
Forge tests help prove whether a suspected issue is real. They are useful for exploit reproduction, fix validation, fuzzing, invariant checks, and fork-based protocol simulations.
Auditors prefer tests that show attacker actions and final impact clearly.
Red flags in code
-
Tests assert no final state or balance changes.
-
Mocks hide important integration behavior.
-
Fork tests do not pin block numbers.
-
Negative cases and unauthorized callers are missing.
-
Fuzz tests exist but do not assert meaningful properties.
How to test or review it
-
Use
vm.prankto test different callers. -
Use
vm.expectRevertfor forbidden actions. -
Use traces with
-vvvvto debug exploit flow. -
Add fuzz tests for input-heavy code.
-
Add regression tests for every fixed audit finding.
Keep learning this topic
Foundry
Foundry is a Solidity development and testing toolkit with Forge for tests, Cast for RPC calls, Anvil for local chains, and Chisel for a Solidity REPL.
Fuzz Testing
Fuzz testing sends many generated inputs through smart contract code to find edge cases, unexpected reverts, broken assumptions, and state transitions that normal unit tests miss.
Invariant Testing
Invariant testing checks that important smart contract properties stay true across many generated call sequences, actors, and state transitions.
Practice this in real audit scenarios
Definitions help, but auditors need reps. SCH turns concepts like Forge Test into exploit labs, code review habits, and report-writing practice.
Start the free trial or see the full smart contract auditing course.