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.

solidbyte.testing.fixtures.std_tx(tx: Union[dict, attrdict.dictionary.AttrDict, web3.datastructures.AttributeDict])[source]

Create a test transaction with default gas and gasPrice

Parameters:tx – (dict) representation of an Ethereum transaction
Returns:(dict) representation of an Ethereum transaction with defaults

has_event

Function to check if a receipt contains an event.

solidbyte.testing.fixtures.has_event(web3contract: web3.contract.Contract, event_name: str, rcpt: Union[dict, attrdict.dictionary.AttrDict, web3.datastructures.AttributeDict]) → bool[source]

Check if a receipt contains an event by name

Parameters:
  • web3contract – (web3.contract.Contract) The contract that has the event ABI we are looking for.
  • event_name – (str) the name of the event
  • rcpt – (dict) object of the transaction receipt
Returns:

(bool)

get_event

Function to pull the event data from a receipt.

solidbyte.testing.fixtures.get_event(web3contract: web3.contract.Contract, event_name: str, rcpt: Union[dict, attrdict.dictionary.AttrDict, web3.datastructures.AttributeDict]) → Optional[web3.datastructures.AttributeDict][source]

Return the event data from a transaction receipt

Parameters:
  • web3contract – (web3.contract.Contract) The contract that has the event ABI we are looking for.
  • event_name – (str) the name of the event
  • rcpt – (dict) object of the transaction receipt
Returns:

(dict) the event data

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