Fixture and Marker Reference

Brownie includes custom fixtures and markers that can be used when testing your project.

Session Fixtures

These fixtures provide quick access to Brownie objects that are frequently used during testing. If you are unfamiliar with these objects, you may wish to read the documentation listed under “Core Functionality” in the table of contents.

accounts

Yields an Accounts container for the active project, used to interact with your local accounts.

1
2
def test_account_balance(accounts):
    assert accounts[0].balance() == "100 ether"
a

Short form of the accounts fixture.

1
2
def test_account_balance(a):
    assert a[0].balance() == "100 ether"
chain

Yields an Chain object, used to access block data and interact with the local test chain.

1
2
3
4
5
6
7
def test_account_balance(accounts, chain):
    balance = accounts[1].balance()
    accounts[0].transfer(accounts[1], "10 ether")
    assert accounts[1].balance() == balance + "10 ether"

    chain.reset()
    assert accounts[1].balance() == balance
Contract

Yields the Contract class, used to interact with contracts outside of the active project.

1
2
3
@pytest.fixture(scope="session")
def dai(Contract):
    yield Contract.from_explorer("0x6B175474E89094C44Da98b954EedeAC495271d0F")
history

Yields a TxHistory container for the active project, used to access transaction data.

1
2
3
def test_account_balance(accounts, history):
    accounts[0].transfer(accounts[1], "10 ether")
    assert len(history) == 1
interface

Yields the InterfaceContainer object for the active project, which provides access to project interfaces.

1
2
3
@pytest.fixture(scope="session")
def dai(interface):
    yield interface.Dai("0x6B175474E89094C44Da98b954EedeAC495271d0F")
pm

Callable fixture that provides access to Project objects, used for testing against installed packages.

1
2
3
4
@pytest.fixture(scope="module")
def compound(pm, accounts):
    ctoken = pm('defi.snakecharmers.eth/compound@1.1.0').CToken
    yield ctoken.deploy({'from': accounts[0]})
state_machine

Yields the state_machine method, used for running a stateful test.

1
2
3
4
def test_stateful(Token, accounts, state_machine):
    token = Token.deploy("Test Token", "TST", 18, 1e23, {'from': accounts[0]})

    state_machine(StateMachine, accounts, token)
web3

Yields a Web3 object.

1
2
3
4
def test_account_balance(accounts, web3):
    height = web3.eth.blockNumber
    accounts[0].transfer(accounts[1], "10 ether")
    assert web3.eth.blockNumber == height + 1

Contract Fixtures

Brownie creates dynamically named fixtures to access each ContractContainer object within a project. Fixtures are generated for all deployable contracts and libraries.

For example - if your project contains a contract named Token, there will be a Token fixture available.

1
2
3
def test_token_deploys(Token, accounts):
    token = accounts[0].deploy(Token, "Test Token", "TST", 18, 1e24)
    assert token.name() == "Test Token"

Isolation Fixtures

Isolation fixtures are used ensure a clean test environment when running tests, and to prevent the results of a test from affecting subsequent tests. See Isolation Fixtures for information on how to use these fixtures.

module_isolation

Resets the local chain before running and after completing the test module.

fn_isolation

Takes a snapshot of the chain before running a test and reverts to it after the test completes.

Markers

Brownie provides the following markers for use within your tests:

pytest.mark.require_network(network_name)

Mark a test so that it only runs if the active network is named network_name. This is useful when you have some tests intended for a local development environment and others for a forked mainnet.

1
2
3
@pytest.mark.require_network("mainnet-fork")
def test_almost_in_prod():
    pass
pytest.mark.no_call_coverage

Only evaluate coverage for transactions made during this test, not calls.

This marker is useful for speeding up slow tests that involve many calls to the same view method.

1
2
3
4
5
6
7
8
def test_normal(token):
    # during coverage analysis this call is handled as a transaction
    assert token.balanceOf(accounts[0]) == 900

@pytest.mark.no_call_coverage
def test_no_call_cov(Token):
    # this call is handled as a call, the test execution is quicker
    assert token.balanceOf(accounts[1]) == 100
pytest.mark.skip_coverage

Skips a test if coverage evaluation is active.

1
2
3
@pytest.mark.skip_coverage
def test_heavy_lifting():
    pass