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.1def test_account_balance(accounts): 2 assert accounts[0].balance() == "100 ether"
- a
Short form of the
accounts
fixture.1def test_account_balance(a): 2 assert a[0].balance() == "100 ether"
- chain
Yields a
Chain
object, used to access block data and interact with the local test chain.1def test_account_balance(accounts, chain): 2 balance = accounts[1].balance() 3 accounts[0].transfer(accounts[1], "10 ether") 4 assert accounts[1].balance() == balance + "10 ether" 5 6 chain.reset() 7 assert accounts[1].balance() == balance
- Contract
Yields the
Contract
class, used to interact with contracts outside of the active project.1@pytest.fixture(scope="session") 2def dai(Contract): 3 yield Contract.from_explorer("0x6B175474E89094C44Da98b954EedeAC495271d0F")
- history
Yields a
TxHistory
container for the active project, used to access transaction data.1def test_account_balance(accounts, history): 2 accounts[0].transfer(accounts[1], "10 ether") 3 assert len(history) == 1
- interface
Yields the
InterfaceContainer
object for the active project, which provides access to project interfaces.1@pytest.fixture(scope="session") 2def dai(interface): 3 yield interface.Dai("0x6B175474E89094C44Da98b954EedeAC495271d0F")
- pm
Callable fixture that provides access to
Project
objects, used for testing against installed packages.1@pytest.fixture(scope="module") 2def compound(pm, accounts): 3 ctoken = pm('defi.snakecharmers.eth/compound@1.1.0').CToken 4 yield ctoken.deploy({'from': accounts[0]})
- state_machine
Yields the
state_machine
method, used for running a stateful test.1def test_stateful(Token, accounts, state_machine): 2 token = Token.deploy("Test Token", "TST", 18, 1e23, {'from': accounts[0]}) 3 4 state_machine(StateMachine, accounts, token)
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.
1def test_token_deploys(Token, accounts): 2 token = accounts[0].deploy(Token, "Test Token", "TST", 18, 1e24) 3 assert token.name() == "Test Token"
Isolation Fixtures
Isolation fixtures are used to 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@pytest.mark.require_network("mainnet-fork") 2def test_almost_in_prod(): 3 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.
1def test_normal(token): 2 # during coverage analysis this call is handled as a transaction 3 assert token.balanceOf(accounts[0]) == 900 4 5@pytest.mark.no_call_coverage 6def test_no_call_cov(Token): 7 # this call is handled as a call, the test execution is quicker 8 assert token.balanceOf(accounts[1]) == 100
- pytest.mark.skip_coverage
Skips a test if coverage evaluation is active.
1@pytest.mark.skip_coverage 2def test_heavy_lifting(): 3 pass