블록체인 (Block Chain)/이더리움

[브라우니 (Brownie)] 16. 픽스처 및 마커 참조

gsroot 2023. 6. 18. 17:27
728x90
반응형
SMALL

 

브라우니에는 프로젝트를 테스트할 때 사용할 수 있는 사용자 정의 fixturesmarkers가 포함되어 있습니다.

세션 fixtures

이러한 fixtures는 테스트 중 자주 사용되는 Brownie 객체에 빠르게 액세스할 수 있도록 제공됩니다. 이러한 객체에 익숙하지 않은 경우 목차에서 "핵심 기능"으로 나열된 문서를 읽으시기 바랍니다.

accounts

활성 프로젝트에 대한 Accounts 컨테이너를 생성하여 로컬 계정과 상호 작용하는 데 사용됩니다.

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

a

accounts 픽스처의 약식 형태.

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

chain

로컬 테스트 체인과 상호 작용하고 블록 데이터에 액세스하는 데 사용되는 Chain 객체를 반환합니다.

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

Contract 클래스는 활성화된 프로젝트 외부의 계약과 상호 작용하는 데 사용됩니다.

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

history

활성 프로젝트에 대한 TxHistory 컨테이너를 생성하고, 트랜잭션 데이터에 접근하는 데 사용됩니다.

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

interface

활성 프로젝트에 대한 InterfaceContainer 개체를 반환하여 프로젝트 인터페이스에 대한 액세스를 제공합니다.

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

pm

설치된 패키지를 대상으로 테스트하는 데 사용되는 Project 객체에 대한 액세스를 제공하는 호출 가능한 픽스처입니다.

@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

state_machine 메서드는 Stateful 테스트 실행에 사용됩니다.

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

    state_machine(StateMachine, accounts, token)

web3

Web3 객체를 반환합니다.

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

계약 픽스처

Brownie는 프로젝트 내의 각 ContractContainer 객체에 액세스하기 위해 동적으로 이름이 지정된 픽스처를 생성합니다. 배포 가능한 모든 계약 및 라이브러리에 대해 픽스처가 생성됩니다.

예를 들어 - 프로젝트에 Token이라는 계약이 포함되어 있으면 Token 픽스처를 사용할 수 있습니다.

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

격리 픽스처

격리 픽스처는 테스트를 실행할 때 깨끗한 테스트 환경을 보장하고 테스트 결과가 다음 테스트에 영향을 미치지 않도록하는 데 사용됩니다. 이러한 픽스처를 사용하는 방법에 대한 정보는 격리 픽스처(Isolation Fixtures)을 참조하십시오.

module_isolation

테스트 모듈을 실행하기 전에 로컬 체인을 재설정하고 테스트가 완료된 후에도 재설정합니다.

fn_isolation

테스트를 실행하기 전에 체인의 스냅샷을 가져오고 테스트가 완료된 후에 그것으로 되돌립니다.

마커

Brownie는 다음과 같은 마커를 제공하여 테스트에서 사용합니다:

pytest.mark.require_network(network_name)

활성 네트워크가 network_name으로 명명된 경우에만 해당 테스트가 실행되도록 지정합니다. 이는 개발 환경에서 몇 가지 테스트를 수행하고 포크된 메인넷에서 다른 테스트를 수행하는 경우에 유용합니다.

@pytest.mark.require_network("mainnet-fork")
def test_almost_in_prod():
    pass

pytest.mark.no_call_coverage

이 표시는 호출이 아닌 이 테스트 중에 수행된 트랜잭션만 커버리지를 평가합니다. 이 마커는 동일한 뷰 메서드에 대한 많은 호출을 포함하는 느린 테스트를 가속화하는 데 유용합니다.

def test_normal(token):
    # during coverage analysis this call is handled as a transactionassert 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 quickerassert token.balanceOf(accounts[1]) == 100

pytest.mark.skip_coverage

커버리지 평가가 활성화되어 있는 경우 테스트를 건너뛴다.

@pytest.mark.skip_coverage
def test_heavy_lifting():
    pass

 

728x90
반응형
LIST