Skip to main content

The testing package containing tools to test Web3-based projects

Project description

pytest-evm

Croco Logo

The testing package containing tools to test Web3-based projects

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

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 NetworkOrInfo
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 test performs 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.

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

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/blnkoff/pytest-evm.git

To install the package from PyPi you can use:

pip install pytest-evm

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_evm-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

pytest_evm-0.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_evm-0.1.1.tar.gz.

File metadata

  • Download URL: pytest_evm-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pytest_evm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f32e4724d730132a1348c16b2da14befff9d0825b425814ebee1bf6970adbc6a
MD5 8724f2168a06878d1fea8b37d1d3652c
BLAKE2b-256 33e6936ff0e4d469c43fed06626b3c86ecfdf0b9eb599b6e071e68b5f7cc91b5

See more details on using hashes here.

File details

Details for the file pytest_evm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_evm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pytest_evm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da786b227c7b44b8b4f511c70c7a09f6c20b8fb1945950265280694dfb290b8b
MD5 0d0de40edcfdb92bed3b41903fca8185
BLAKE2b-256 c62f9cddc5999f6645a728f533af23ec6542fa4989c878595b343e01790d4e3f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page