Package: pytest-evm
pytest-evmThe testing package containing tools to test Web3-based projectsTelegram channelBug reportsPackage's source code is made available under theMIT LicenseQuick StartThere are few features simplifying your testing with pytest:FixturesTest ReportingUsage ExampleFixturesmake_walletThis fixture simplify creating wallet instances as fixtures. Wallet instances are fromevm-walletpackageimportosimportpytestfromtypingimportOptionalfromevm_wallet.typesimportNetworkOrInfofromevm_walletimportAsyncWallet,Wallet@pytest.fixture(scope="session")defmake_wallet():def_make_wallet(network:NetworkOrInfo,private_key:Optional[str]=None,is_async:bool=True):ifnotprivate_key:private_key=os.getenv('TEST_PRIVATE_KEY')returnAsyncWallet(private_key,network)ifis_asyncelseWallet(private_key,network)return_make_walletYou 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, usingNetworkOrInfo.importpytest@pytest.fixturedefwallet(make_wallet):returnmake_wallet('Optimism')As you can see, a private key wasn't passed. This because of by-defaultmake_wallettakes it from
environment variableTEST_PRIVATE_KEY. You can set environment variables using extra-packagepython-dotenv.# conftest.pyimportpytestfromdotenvimportload_dotenvload_dotenv()@pytest.fixture(scope="session")defwallet(make_wallet):returnmake_wallet('Polygon')Here is the content of .env file# .envTEST_PRIVATE_KEY=0x0000000000000000000000000000000000000000You can installpython-dotenvalong withpytest-evm:pipinstallpytest-evm[dotenv]zero_addressThis fixture returns ZERO_ADDRESS valueimportpytestfromevm_walletimportZERO_ADDRESS@pytest.fixture(scope="session")defzero_address():returnZERO_ADDRESSeth_amountThis fixture returns 0.001 ETH in Wei, which is the most using minimal value for testsimportpytestfromweb3importAsyncWeb3@pytest.fixture(scope="session")defeth_amount():amount=AsyncWeb3.to_wei(0.001,'ether')returnamountTest ReportingIf your test performs one transaction, you can automaticallyasserttransaction status and get useful report after test,
if it completed successfully. To do this, you need to add markpytest.mark.txto your test.importpytest@pytest.mark.tx@pytest.mark.asyncioasyncdeftest_transaction(wallet,eth_amount):recipient='0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f'params=awaitwallet.build_transaction_params(eth_amount,recipient=recipient)returnawaitwallet.transact(params)After test, you get similar report:Usage ExampleHere is example of testing withpytest-evm:importpytestclassTestBridge:@pytest.mark.tx@pytest.mark.asyncioasyncdeftest_swap(self,wallet,eth_amount,bridge,destination_network):returnawaitbridge.swap(eth_amount,destination_network)@pytest.mark.tx@pytest.mark.asyncioasyncdeftest_swap_to_eth(self,wallet,eth_amount,bridge):returnawaitbridge.swap_to_eth(eth_amount)@pytest.fixturedefwallet(self,make_wallet):returnmake_wallet('Optimism')@pytest.fixturedefbridge(self,wallet):returnBridge(wallet)@pytest.fixturedefdestination_network(self):return'Arbitrum'Installing pytest-evmTo install the package from GitHub you can use:pipinstallgit+https://github.com/blnkoff/pytest-evm.gitTo install the package from PyPi you can use:pipinstallpytest-evm