Testing Your Contracts¶
Testing your contracts with SolidByte is pretty straight forward. SolidByte uses pytest as a test runner and provides some useful fixtures to help ease testing.
Fixtures¶
contracts
¶
The contracts
fixture is an attrdict.AttrDict
instance with all of your
deployed contracts as web3.contract.Contract
instances.
web3
¶
This is the initialized instance of web3.Web3
that should already be
connected to whatever network you gave on the CLI.
local_accounts
¶
list
of addresses of the known local accounts.
std_tx
¶
Function to update a transaction dict with standard values.
has_event
¶
Function to check if a receipt contains an event.
get_event
¶
Function to pull the event data from a receipt.
Example Test¶
Here’s an example test provided with the erc20
template:
def test_erc20(web3, contracts):
print("contracts: ", contracts)
""" We're just going to test to make sure the contracts fixture is being
populated with deployed contract instances
"""
assert 'MyERC20' in contracts, "Contract not deployed"
assert hasattr(contracts.MyERC20, 'address')
assert type(contracts.MyERC20.address) == str
assert len(contracts.MyERC20.address) == 42
assert contracts.MyERC20.address[:2] == '0x'
assert len(web3.eth.accounts) > 0
admin = web3.eth.accounts[0]
# Deployed version should have no tokens to start
assert contracts.MyERC20.functions.balanceOf(admin).call() == 0
assert contracts.MyERC20.functions.totalSupply().call() > 0