pytest-evm


Namepytest-evm JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/blnkoff/pytest-evm
SummaryThe testing package containing tools to test Web3-based projects
upload_time2024-04-22 19:49:03
maintainerNone
docs_urlNone
authorAlexey
requires_python<4.0,>=3.11
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-evm

[![Croco Logo](https://i.ibb.co/G5Pjt6M/logo.png)](https://t.me/crocofactory)

The testing package containing tools to test Web3-based projects

- **[Telegram channel](https://t.me/crocofactory)**
- **[Bug reports](https://github.com/blnkoff/pytest-evm/issues)**

Package's source code is made available under the [MIT License](LICENSE)

# Quick Start
There are few features simplifying your testing with pytest:
- **[Fixtures](#fixtures)**
- **[Test Reporting](#test-reporting)**
- **[Usage Example](#usage-example)**

## Fixtures

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

```python
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`. 

```python
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`.

```python
# 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

```shell
# .env

TEST_PRIVATE_KEY=0x0000000000000000000000000000000000000000
```

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

```shell
pip install pytest-evm[dotenv]
```

### zero_address
This fixture returns ZERO_ADDRESS value      

```python
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 

```python
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**

```python
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](https://i.ibb.co/h98dNPL/Screenshot-2024-04-22-at-22-41-19.png)
         
## Usage Example
Here is example of testing with `pytest-evm`:

```python
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:

```shell
pip install git+https://github.com/blnkoff/pytest-evm.git
```

To install the package from PyPi you can use:
```shell
pip install pytest-evm
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/blnkoff/pytest-evm",
    "name": "pytest-evm",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": null,
    "author": "Alexey",
    "author_email": "axbelenkov@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8b/e6/d49db61438e818042384d388c1ab22306b2a441ee0217f6d76eee6445640/pytest_evm-0.2.0.tar.gz",
    "platform": null,
    "description": "# pytest-evm\n\n[![Croco Logo](https://i.ibb.co/G5Pjt6M/logo.png)](https://t.me/crocofactory)\n\nThe testing package containing tools to test Web3-based projects\n\n- **[Telegram channel](https://t.me/crocofactory)**\n- **[Bug reports](https://github.com/blnkoff/pytest-evm/issues)**\n\nPackage's source code is made available under the [MIT License](LICENSE)\n\n# Quick Start\nThere are few features simplifying your testing with pytest:\n- **[Fixtures](#fixtures)**\n- **[Test Reporting](#test-reporting)**\n- **[Usage Example](#usage-example)**\n\n## Fixtures\n\n### make_wallet\nThis fixture simplify creating wallet instances as fixtures. Wallet instances are from `evm-wallet` package\n\n```python\nimport os\nimport pytest\nfrom typing import Optional\nfrom evm_wallet.types import NetworkOrInfo\nfrom evm_wallet import AsyncWallet, Wallet\n\n@pytest.fixture(scope=\"session\")\ndef make_wallet():\n    def _make_wallet(network: NetworkOrInfo, private_key: Optional[str] = None, is_async: bool = True):\n        if not private_key:\n            private_key = os.getenv('TEST_PRIVATE_KEY')\n        return AsyncWallet(private_key, network) if is_async else Wallet(private_key, network)\n\n    return _make_wallet\n```\n\nYou can specify whether your wallet should be of async or sync version. Instead of specifying RPC, you only have to provide\nchain's name. You can also specify a custom Network, using `NetworkOrInfo`. \n\n```python\nimport pytest\n\n@pytest.fixture\ndef wallet(make_wallet):\n    return make_wallet('Optimism')\n```\n\nAs you can see, a private key wasn't passed. This because of by-default `make_wallet` takes it from\nenvironment variable `TEST_PRIVATE_KEY`. You can set environment variables using extra-package `python-dotenv`.\n\n```python\n# conftest.py\n\nimport pytest\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\n\n@pytest.fixture(scope=\"session\")\ndef wallet(make_wallet):\n    return make_wallet('Polygon')\n```\n \nHere is the content of .env file\n\n```shell\n# .env\n\nTEST_PRIVATE_KEY=0x0000000000000000000000000000000000000000\n```\n\nYou can install `python-dotenv` along with `pytest-evm`:\n\n```shell\npip install pytest-evm[dotenv]\n```\n\n### zero_address\nThis fixture returns ZERO_ADDRESS value      \n\n```python\nimport pytest\nfrom evm_wallet import ZERO_ADDRESS\n\n@pytest.fixture(scope=\"session\")\ndef zero_address():\n    return ZERO_ADDRESS\n```\n\n### eth_amount\nThis fixture returns 0.001 ETH in Wei, which is the most using minimal value for tests \n\n```python\nimport pytest\nfrom web3 import AsyncWeb3\n\n@pytest.fixture(scope=\"session\")\ndef eth_amount():\n    amount = AsyncWeb3.to_wei(0.001, 'ether')\n    return amount\n```\n\n## Test Reporting\nIf your want to test one transaction, you can automatically `assert` transaction status and get useful report after test,\nif it completed successfully. To do this, you need to add mark `pytest.mark.tx` to your test and you must **return \ntransaction hash in test**\n\n```python\nimport pytest\n\n@pytest.mark.tx\n@pytest.mark.asyncio\nasync def test_transaction(wallet, eth_amount):\n    recipient = '0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f'\n    params = await wallet.build_transaction_params(eth_amount, recipient=recipient)\n    return await wallet.transact(params)\n```\n\nAfter test, you get similar report:\n\n![Test Report](https://i.ibb.co/h98dNPL/Screenshot-2024-04-22-at-22-41-19.png)\n         \n## Usage Example\nHere is example of testing with `pytest-evm`:\n\n```python\nimport pytest\nfrom bridge import Bridge\n\nclass TestBridge:\n    @pytest.mark.tx\n    @pytest.mark.asyncio\n    async def test_swap(self, wallet, eth_amount, bridge, destination_network):\n        return await bridge.swap(eth_amount, destination_network)\n\n    @pytest.mark.tx\n    @pytest.mark.asyncio\n    async def test_swap_to_eth(self, wallet, eth_amount, bridge):\n        return await bridge.swap_to_eth(eth_amount)\n\n    @pytest.fixture\n    def wallet(self, make_wallet):\n        return make_wallet('Optimism')\n\n    @pytest.fixture\n    def bridge(self, wallet):\n        return Bridge(wallet)\n\n    @pytest.fixture\n    def destination_network(self):\n        return 'Arbitrum'\n```\n\n# Installing pytest-evm\nTo install the package from GitHub you can use:\n\n```shell\npip install git+https://github.com/blnkoff/pytest-evm.git\n```\n\nTo install the package from PyPi you can use:\n```shell\npip install pytest-evm\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "The testing package containing tools to test Web3-based projects",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/blnkoff/pytest-evm",
        "Repository": "https://github.com/blnkoff/pytest-evm"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e219aed737c552e2b0f113ff3b8cec46a6c7aabf6cc3e18e2a73a056a864ce4c",
                "md5": "be84bad6b4d0a1ee600341096c52a9ba",
                "sha256": "1028faeb7e1331486d97c09181a29f2eb070a237930c5d038da3dc7c6c2fae32"
            },
            "downloads": -1,
            "filename": "pytest_evm-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "be84bad6b4d0a1ee600341096c52a9ba",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 6994,
            "upload_time": "2024-04-22T19:49:01",
            "upload_time_iso_8601": "2024-04-22T19:49:01.860027Z",
            "url": "https://files.pythonhosted.org/packages/e2/19/aed737c552e2b0f113ff3b8cec46a6c7aabf6cc3e18e2a73a056a864ce4c/pytest_evm-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8be6d49db61438e818042384d388c1ab22306b2a441ee0217f6d76eee6445640",
                "md5": "c9ea7c9cfbf47349de44c6fc27c4546b",
                "sha256": "3e07f9fd71e4e9d00a5c6d59e7b88cd3f4f6e5122e9b46d96bfbe8c1d21d8a0f"
            },
            "downloads": -1,
            "filename": "pytest_evm-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c9ea7c9cfbf47349de44c6fc27c4546b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 5334,
            "upload_time": "2024-04-22T19:49:03",
            "upload_time_iso_8601": "2024-04-22T19:49:03.755205Z",
            "url": "https://files.pythonhosted.org/packages/8b/e6/d49db61438e818042384d388c1ab22306b2a441ee0217f6d76eee6445640/pytest_evm-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-22 19:49:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "blnkoff",
    "github_project": "pytest-evm",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pytest-evm"
}
        
Elapsed time: 9.52518s