Pytest Fixtures Reference

Brownie provides fixtures to allow you to interact with your project during tests. To use a fixture, add an argument with the same name to the inputs of your test function.

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.

Coverage Fixtures

Coverage fixtures alter the behaviour of tests when coverage evaluation is active. They are useful for tests with many repetitive functions, to avoid the slowdown caused by debug_traceTransaction queries.

no_call_coverage

Coverage evaluation will not be performed on called contact methods during this test.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import pytest

@pytest.fixture(scope="module", autouse=True)
def token(Token, accounts):
    t = accounts[0].deploy(Token, "Test Token", "TST", 18, 1000)
    t.transfer(accounts[1], 100, {'from': accounts[0]})
    yield t

def test_normal(token):
    # this call is handled as a transaction, coverage is evaluated
    assert token.balanceOf(accounts[0]) == 900

def test_no_call_cov(Token, no_call_coverage):
    # this call happens normally, no coverage evaluation
    assert token.balanceOf(accounts[1]) == 100
skip_coverage

Skips a test if coverage evaluation is active.

1
2
def test_heavy_lifting(skip_coverage):
    pass