Repository navigation
Is there a way to snapshot storage between tests instead of redeploying every time? #16998
|
My test suite deploys a bunch of contracts in setUp() and each test takes ~2 seconds because of it. The contracts are the same across all tests — only the test actions differ. I thought Is there a way to deploy once and reuse that state across the whole test file? Something like a shared fixture that all tests fork from? My suite has 40+ tests and it takes over a minute to run which is brutal for iteration. Using forge 1.1.x if that matters. |
Replies: 2 comments
|
Not in the “deploy once for the whole test file” sense. In Forge’s normal Solidity test model, So this works for branching inside one test: function test_manyBranches() public {
uint256 snap = vm.snapshotState();
// branch A
// ...
vm.revertToState(snap);
// branch B from the same setup state
// ...
}but it is not a way to do: If the expensive part is deployment, the usual Foundry options are:
In short: |
|
setUp() with snapshot + revertTo in each test is exactly what I needed, cuts suite time in half. thanks |
Not in the “deploy once for the whole test file” sense. In Forge’s normal Solidity test model,
setUp()runs before each test, so each test gets its own setup phase. The state snapshot cheatcodes are for snapshotting and restoring EVM state inside a test execution context; they are not a cross-test fixture cache that survives into the next test.So this works for branching inside one test:
but it is not a way to do:
If the expens…