evm_snapshot_revert

Documentation for eth_defi.testing.evm_snapshot_fixture.evm_snapshot_revert function.

evm_snapshot_revert(fork, *, strict=False)

Snapshot EVM state before, revert after — generator helper for autouse fixtures.

Yields once after taking the snapshot, then reverts on resume. Designed to be used with yield from inside a per-file @pytest.fixture(autouse=True) so each test sees a clean state on a module-scope Anvil fork.

Parameters
  • fork (Union[eth_defi.provider.anvil.AnvilLaunch, str]) – JSON-RPC URL string or an object with a json_rpc_url attribute, typically AnvilLaunch. The fixture that yields fork should itself be scope="module" or scope="session" — otherwise the snapshot/revert dance buys nothing.

  • strict (bool) – Fail fixture teardown if the snapshot cannot be reverted, exposing lost isolation. This does not dispose of the fork or stop later tests; callers must recover or discard a damaged fork. The default logs a warning.

Returns

Generator yielding None once, then performing the revert on resume.

Raises

RuntimeError – If strict revert fails, or the evm_snapshot RPC call returns a non-result (e.g. an older Anvil build or a non-Anvil backend snuck in via a different fixture).

Return type

collections.abc.Iterator[None]

Note

Snapshot behaviour depends on the backend. Tests requiring an exact timestamp for the next mined block should set it explicitly with evm_setNextBlockTimestamp after restoring their baseline.