forge-Tests
Solidity-Tests
forge-Tests ist eine kostenlose Web3 & DApp Development Fundamentals-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Web3 & DApp Development Fundamentals-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Tests Are Contracts
In Foundry a test file is a Solidity contract that inherits from Test in the forge-std library. Each public function whose name starts with test is run as an individual test case.
Test files live in test/ and conventionally use the .t.sol suffix.
import {Test} from "forge-std/Test.sol";
contract CounterTest is Test {
function testInitialZero() public {
// ...
}
}The setUp Function
The special setUp() function runs before every test, giving each test a fresh, isolated state. Deploy your contracts and seed any fixtures here.
Because state resets between tests, one test can never accidentally depend on another.
Counter counter;
function setUp() public {
counter = new Counter();
}Assertions
forge-std provides assertion helpers that fail the test with a clear message when they do not hold:
assertEq(a, b)— equalityassertTrue(cond)/assertFalse(cond)assertGt,assertLt,assertGe,assertLe
function testIncrement() public {
counter.increment();
assertEq(counter.number(), 1);
}Running Tests
Run the whole suite with forge test. Filter by name with --match-test or by contract with --match-contract. Increase verbosity with -v flags to see logs and traces.
$ forge test
$ forge test --match-test testIncrement -vvvExpecting Reverts
Use vm.expectRevert immediately before a call that should fail. The test passes only if the next call reverts, optionally matching a specific error message or custom error.
function testOnlyOwner() public {
vm.prank(address(0xBEEF));
vm.expectRevert("not owner");
counter.adminReset();
}Impersonating Accounts
vm.prank(addr) sets msg.sender for the very next call. vm.startPrank(addr) applies it to all calls until vm.stopPrank(). This lets you test access control without deploying from many keys.
vm.startPrank(alice);
token.approve(spender, 100);
token.transfer(bob, 50);
vm.stopPrank();Manipulating Balances and Time
Cheatcodes let you control the environment:
vm.deal(addr, amount)— set an account's ETH balancevm.warp(timestamp)— set block.timestampvm.roll(blockNum)— set block.number
These make time-dependent and balance-dependent logic easy to test deterministically.
vm.deal(alice, 10 ether);
vm.warp(block.timestamp + 7 days);
vm.roll(block.number + 100);Testing Events
vm.expectEmit declares which event fields to check, followed by the expected event, then the call that should emit it. The booleans select which topics and data to match.
vm.expectEmit(true, true, false, true);
emit Transfer(alice, bob, 100);
token.transfer(bob, 100);Labels and Logs
vm.label(addr, name) gives addresses readable names in traces. console.log from forge-std prints debug output during tests when run with sufficient verbosity.
import {console} from "forge-std/console.sol";
vm.label(alice, "Alice");
console.log("balance:", token.balanceOf(alice));Fork Testing
Foundry can fork a live network so your tests run against real deployed contracts and state. Set an RPC URL and use vm.createSelectFork to pin a block.
This is invaluable for testing integrations with protocols like Uniswap or Aave without redeploying them.
function setUp() public {
vm.createSelectFork(vm.rpcUrl("mainnet"), 19000000);
}Test Organization
Good practices for forge tests:
- One behavior per test function
- Descriptive names like
testRevertWhenNotOwner - Shared setup in
setUp() - Use prank for access control, warp for time logic
- Add fork tests for external integrations
Quick Check
What is the purpose of the setUp() function in a Foundry test contract?
Recap
You learned forge testing:
- Tests are Solidity contracts inheriting
Test; functions start withtest setUp()gives fresh state per test- Assertions:
assertEq,assertTrue, etc. - Cheatcodes:
prank,deal,warp,expectRevert,expectEmit - Fork tests run against live network state
Next: fuzzing and invariant testing.
Häufig gestellte Fragen
Ist die Lektion „forge-Tests“ kostenlos?
Ja — der vollständige Text von „forge-Tests“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web3 & DApp Development Fundamentals-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „forge-Tests“?
Solidity-Tests Du übst Web3 & DApp Development Fundamentals mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Web3 & DApp Development Fundamentals zu starten?
Keine Vorkenntnisse erforderlich. Web3 & DApp Development Fundamentals auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „forge-Tests“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Web3 & DApp Development Fundamentals-Lektion Code schreiben und ausführen?
Ja. Jede Web3 & DApp Development Fundamentals-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.