AnvilSnapshotState
Documentation for eth_defi.provider.anvil.AnvilSnapshotState Python class.
- class AnvilSnapshotState
Bases:
objectMutable reset point for a shared Anvil backend.
This helper is designed for pytest suites that keep one Anvil process alive across multiple tests and reset it cheaply using
evm_snapshot/evm_revertinstead of relaunching the fork each time.Use
create_anvil_snapshot_state()to take the initial snapshot andreset_anvil_snapshot()to restore it between tests.Note
Only use this pattern in self-contained test modules or conftest files where the
web3fixture is not overridden by sibling modules. Placing anautouse=Truerestore fixture in a sharedconftest.pycausesScopeMismatcherrors when other test modules in the same directory overrideweb3with function scope (e.g. for a different chain). Additionally, module-scoped Anvil forks combined with repeated snapshot/revert cycles can hang on CI runners underpytest-xdistparallel execution, likely due to Anvil process responsiveness degradation after many revert cycles.Example:
import pytest from eth_defi.provider.anvil import AnvilSnapshotState, create_anvil_snapshot_state, reset_anvil_snapshot @pytest.fixture(scope="module") def deployed_state(web3, deploy_info) -> AnvilSnapshotState: # deploy_info is resolved first so the snapshot captures the # expensive post-deployment baseline return create_anvil_snapshot_state(web3) @pytest.fixture(autouse=True) def restore_deployed_state(web3, deployed_state) -> None: reset_anvil_snapshot(web3, deployed_state)
Attributes summary
snapshot_idMethods summary
__init__(snapshot_id)- __init__(snapshot_id)
- Parameters
snapshot_id (int) –
- Return type
None