Ethereum Tester

0.13.0b1 · active · verified Thu Apr 16

eth-tester is a suite of tools for testing Ethereum applications, providing a clean, consistent API for interacting with various Ethereum backends (e.g., PyEVM, Ganache). It allows developers to simulate blockchain interactions, deploy contracts, and execute transactions in a controlled environment. The current version, 0.13.0b1, is a beta release, indicating active development. Its release cadence is often tied to releases of its core dependencies like PyEVM or major versions of Web3.py.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `EthereumTester` with the built-in `MockBackend`, retrieve accounts, mine blocks, check balances, and send a basic transaction. For full EVM functionality (e.g., real value transfers, contract deployment), a backend like `PyEVMBackend` is required.

from eth_tester import EthereumTester, MockBackend
from eth_utils import encode_hex, decode_hex

# 1. Initialize the EthereumTester with a MockBackend
# The MockBackend is a simple in-memory backend provided by eth-tester.
# It simulates basic blockchain interactions for testing purposes.
# For a full EVM, install with 'pip install eth-tester[py-evm]' and use PyEVMBackend.
tester = EthereumTester(backend=MockBackend())

print("EthereumTester initialized with MockBackend.")

# 2. Get available accounts
accounts = tester.get_accounts()
print(f"Available accounts: {[encode_hex(acc) for acc in accounts]}")

# 3. Mine a block
# Many operations, especially transactions, only take effect after a block is mined.
tester.mine_block()
print(f"Block mined. Current block number: {tester.get_block_by_number('latest')['number']}")

# 4. Get an account's balance
# In MockBackend, accounts start with a default balance.
account_0_address = accounts[0]
balance = tester.get_balance(account_0_address)
print(f"Balance of {encode_hex(account_0_address)}: {balance} wei")

# 5. Send a simple 'transaction' (e.g., a call) which is processed after mining.
# Note: MockBackend does not simulate value transfer or EVM state changes without a full EVM backend.
# This example primarily demonstrates the API for sending a transaction.
transaction_hash = tester.send_transaction({
    'from': account_0_address,
    'to': accounts[1], # Sending to another account (conceptual in MockBackend)
    'gas_price': 0,
    'gas': 21000,
    'value': 0,
    'data': b''
})
print(f"Transaction sent (hash: {encode_hex(transaction_hash)})")

# Mine another block for the transaction to be included
tester.mine_block()
print(f"Block mined. Current block number: {tester.get_block_by_number('latest')['number']}")

view raw JSON →