|
1 | 1 | # pytest-evm |
| 2 | + |
| 3 | +[](https://t.me/crocofactory) |
| 4 | + |
2 | 5 | The testing package containing tools to test Web3-based projects |
| 6 | + |
| 7 | +- **[Telegram channel](https://t.me/crocofactory)** |
| 8 | +- **[Bug reports](https://github.com/blnkoff/pytest-evm/issues)** |
| 9 | + |
| 10 | +Package's source code is made available under the [MIT License](LICENSE) |
| 11 | + |
| 12 | +# Quick Start |
| 13 | +There are few features simplifying your testing with pytest: |
| 14 | +- **[Fixtures](#fixtures)** |
| 15 | +- **[Test Reporting](#test-reporting)** |
| 16 | +- **[Usage Example](#usage-example)** |
| 17 | + |
| 18 | +## Fixtures |
| 19 | + |
| 20 | +### make_wallet |
| 21 | +This fixture simplify creating wallet instances as fixtures. Wallet instances are from `evm-wallet` package |
| 22 | + |
| 23 | +```python |
| 24 | +import os |
| 25 | +import pytest |
| 26 | +from typing import Optional |
| 27 | +from evm_wallet.types import NetworkOrInfo |
| 28 | +from evm_wallet import AsyncWallet, Wallet |
| 29 | + |
| 30 | +@pytest.fixture(scope="session") |
| 31 | +def make_wallet(): |
| 32 | + def _make_wallet(network: NetworkOrInfo, private_key: Optional[str] = None, is_async: bool = True): |
| 33 | + if not private_key: |
| 34 | + private_key = os.getenv('TEST_PRIVATE_KEY') |
| 35 | + return AsyncWallet(private_key, network) if is_async else Wallet(private_key, network) |
| 36 | + |
| 37 | + return _make_wallet |
| 38 | +``` |
| 39 | + |
| 40 | +You can specify whether your wallet should be of async or sync version. Instead of specifying RPC, you only have to provide |
| 41 | +chain's name. You can also specify a custom Network, using `NetworkOrInfo`. |
| 42 | + |
| 43 | +```python |
| 44 | +import pytest |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def wallet(make_wallet): |
| 48 | + return make_wallet('Optimism') |
| 49 | +``` |
| 50 | + |
| 51 | +As you can see, a private key wasn't passed. This because of by-default `make_wallet` takes it from |
| 52 | +environment variable `TEST_PRIVATE_KEY`. You can set environment variables using extra-package `python-dotenv`. |
| 53 | + |
| 54 | +```python |
| 55 | +# conftest.py |
| 56 | + |
| 57 | +import pytest |
| 58 | +from dotenv import load_dotenv |
| 59 | + |
| 60 | +load_dotenv() |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture(scope="session") |
| 64 | +def wallet(make_wallet): |
| 65 | + return make_wallet('Polygon') |
| 66 | +``` |
| 67 | + |
| 68 | +Here is the content of .env file |
| 69 | + |
| 70 | +```shell |
| 71 | +# .env |
| 72 | + |
| 73 | +TEST_PRIVATE_KEY=0x0000000000000000000000000000000000000000 |
| 74 | +``` |
| 75 | + |
| 76 | +You can install `python-dotenv` along with `pytest-evm`: |
| 77 | + |
| 78 | +```shell |
| 79 | +pip install pytest-evm[dotenv] |
| 80 | +``` |
| 81 | + |
| 82 | +### zero_address |
| 83 | +This fixture returns ZERO_ADDRESS value |
| 84 | + |
| 85 | +```python |
| 86 | +import pytest |
| 87 | +from evm_wallet import ZERO_ADDRESS |
| 88 | + |
| 89 | +@pytest.fixture(scope="session") |
| 90 | +def zero_address(): |
| 91 | + return ZERO_ADDRESS |
| 92 | +``` |
| 93 | + |
| 94 | +### eth_amount |
| 95 | +This fixture returns 0.001 ETH in Wei, which is the most using minimal value for tests |
| 96 | + |
| 97 | +```python |
| 98 | +import pytest |
| 99 | +from web3 import AsyncWeb3 |
| 100 | + |
| 101 | +@pytest.fixture(scope="session") |
| 102 | +def eth_amount(): |
| 103 | + amount = AsyncWeb3.to_wei(0.001, 'ether') |
| 104 | + return amount |
| 105 | +``` |
| 106 | + |
| 107 | +## Test Reporting |
| 108 | +If your test performs one transaction, you can automatically `assert` transaction status and get useful report after test, |
| 109 | +if it completed successfully. To do this, you need to add mark `pytest.mark.tx` to your test. |
| 110 | + |
| 111 | +```python |
| 112 | +import pytest |
| 113 | + |
| 114 | +@pytest.mark.tx |
| 115 | +@pytest.mark.asyncio |
| 116 | +async def test_transaction(wallet, eth_amount): |
| 117 | + recipient = '0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f' |
| 118 | + params = await wallet.build_transaction_params(eth_amount, recipient=recipient) |
| 119 | + return await wallet.transact(params) |
| 120 | +``` |
| 121 | + |
| 122 | +After test, you get similar report: |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +## Usage Example |
| 127 | +Here is example of testing with `pytest-evm`: |
| 128 | + |
| 129 | +```python |
| 130 | +import pytest |
| 131 | + |
| 132 | +class TestBridge: |
| 133 | + @pytest.mark.tx |
| 134 | + @pytest.mark.asyncio |
| 135 | + async def test_swap(self, wallet, eth_amount, bridge, destination_network): |
| 136 | + return await bridge.swap(eth_amount, destination_network) |
| 137 | + |
| 138 | + @pytest.mark.tx |
| 139 | + @pytest.mark.asyncio |
| 140 | + async def test_swap_to_eth(self, wallet, eth_amount, bridge): |
| 141 | + return await bridge.swap_to_eth(eth_amount) |
| 142 | + |
| 143 | + @pytest.fixture |
| 144 | + def wallet(self, make_wallet): |
| 145 | + return make_wallet('Optimism') |
| 146 | + |
| 147 | + @pytest.fixture |
| 148 | + def bridge(self, wallet): |
| 149 | + return Bridge(wallet) |
| 150 | + |
| 151 | + @pytest.fixture |
| 152 | + def destination_network(self): |
| 153 | + return 'Arbitrum' |
| 154 | +``` |
| 155 | + |
| 156 | +# Installing pytest-evm |
| 157 | +To install the package from GitHub you can use: |
| 158 | + |
| 159 | +```shell |
| 160 | +pip install git+https://github.com/blnkoff/pytest-evm.git |
| 161 | +``` |
| 162 | + |
| 163 | +To install the package from PyPi you can use: |
| 164 | +```shell |
| 165 | +pip install pytest-evm |
| 166 | +``` |
0 commit comments