Skip to main content

pytest-evm

Croco Logo

PyPi Version PyPI Downloads License Last Commit Development Status

The testing package containing tools to test Web3-based projects

Package's source code is made available under the MIT License

The project is made by the Croco Factory team

Quick Start

There are few features simplifying your testing with pytest:

Fixtures

make_wallet

This fixture simplify creating wallet instances as fixtures. Wallet instances are from evm-wallet package

import os
import pytest
from typing import Optional
from evm_wallet.types import NetworkInfo
from evm_wallet import AsyncWallet, Wallet

@pytest.fixture(scope="session")
def make_wallet():
    def _make_wallet(network: NetworkOrInfo, private_key: Optional[str] = None, is_async: bool = True):
        if not private_key:
            private_key = os.getenv('TEST_PRIVATE_KEY')
        return AsyncWallet(private_key, network) if is_async else Wallet(private_key, network)

    return _make_wallet

You can specify whether your wallet should be of async or sync version. Instead of specifying RPC, you only have to provide chain's name. You can also specify a custom Network, using NetworkOrInfo.

import pytest

@pytest.fixture
def wallet(make_wallet):
    return make_wallet('Optimism')

As you can see, a private key wasn't passed. This because of by-default make_wallet takes it from environment variable TEST_PRIVATE_KEY. You can set environment variables using extra-package python-dotenv.

# conftest.py

import pytest
from dotenv import load_dotenv

load_dotenv()


@pytest.fixture(scope="session")
def wallet(make_wallet):
    return make_wallet('Polygon')

Here is the content of .env file

# .env

TEST_PRIVATE_KEY=0x0000000000000000000000000000000000000000

You can install python-dotenv along with pytest-evm:

pip install pytest-evm[dotenv]

zero_address

This fixture returns ZERO_ADDRESS value

import pytest
from evm_wallet import ZERO_ADDRESS

@pytest.fixture(scope="session")
def zero_address():
    return ZERO_ADDRESS

eth_amount

This fixture returns 0.001 ETH in Wei, which is the most using minimal value for tests

import pytest
from web3 import AsyncWeb3

@pytest.fixture(scope="session")
def eth_amount():
    amount = AsyncWeb3.to_wei(0.001, 'ether')
    return amount

Test Reporting

If your want to test one transaction, you can automatically assert transaction status and get useful report after test, if it completed successfully. To do this, you need to add mark pytest.mark.tx to your test and you must return transaction hash in test

import pytest

@pytest.mark.tx
@pytest.mark.asyncio
async def test_transaction(wallet, eth_amount):
    recipient = '0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f'
    params = await wallet.build_transaction_params(eth_amount, recipient=recipient)
    return await wallet.transact(params)

After test, you get similar report:

Test Report

Usage Example

Here is example of testing with pytest-evm:

import pytest
from bridge import Bridge

class TestBridge:
    @pytest.mark.tx
    @pytest.mark.asyncio
    async def test_swap(self, wallet, eth_amount, bridge, destination_network):
        return await bridge.swap(eth_amount, destination_network)

    @pytest.mark.tx
    @pytest.mark.asyncio
    async def test_swap_to_eth(self, wallet, eth_amount, bridge):
        return await bridge.swap_to_eth(eth_amount)

    @pytest.fixture
    def wallet(self, make_wallet):
        return make_wallet('Optimism')

    @pytest.fixture
    def bridge(self, wallet):
        return Bridge(wallet)

    @pytest.fixture
    def destination_network(self):
        return 'Arbitrum'

Installing pytest-evm

To install the package from GitHub you can use:

pip install git+https://github.com/CrocoFactory/pytest-evm.git

To install the package from PyPi you can use:

pip install pytest-evm

Release files for pytest_evm 0.2.0.post2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest_evm 0.2.0.post2
File Size Uploaded
pytest_evm-0.2.0.post2.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest_evm 0.2.0.post2
File Interpreter ABI Platform
pytest_evm-0.2.0.post2-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / pytest_evm-0.2.0.post2.tar.gz

Download URL pytest_evm-0.2.0.post2.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6771498f0a51e64d1a7074f571445337a795093723f91d758259183d485ae0da
BLAKE2b-256 checksum
How to use checksums
4d6f1fb276536024832fba3b8fd3b948242d0fc7d636e72eeaefbf32e1fc09bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/24.0.0

Release files / pytest_evm-0.2.0.post2-py3-none-any.whl

Download URL pytest_evm-0.2.0.post2-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
665e40e0d54e2a5ca82c27e4a476298d6a1882965d36ec4026a86c06ca158f0b
BLAKE2b-256 checksum
How to use checksums
bb39993778e3e5fde61d30ce53d939b805e3c99d02a566a60b473b44c9d7e7f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/24.0.0

Release history Release notifications | RSS feed

This release

0.2.0.post2 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page