1/5
## More Fixtures We can create a fixture to reduce the code duplication in our tests. We can use a `pytest.fixture` with a `scope="function"` to create a fixture that runs once per test function. Here's how we can write a new fixture called `coffee_funded`: ```python @pytest.fixture(scope="function") def coffee_funded(coffee, account): boa.env.set_balance(account.address, SEND_VALUE) coffee.fund(value=SEND_VALUE) return coffee ``` We can then use this fixture in our tests. For example, in the `test_non_owner_cannot_withdraw` test, instead of setting the balance of the account and funding the contract, we can now use the `coffee_funded` fixture: ```python def test_non_owner_cannot_withdraw(coffee_funded, account): # Arrange with boa.env.prank(RANDOM_USER): with boa.reverts("Not the contract owner!"): coffee_funded.withdraw() ``` We can also update the `test_owner_can_withdraw` test: ```python def test_owner_can_withdraw(coffee_funded, account): with boa.env.prank(coffee_funded.OWNER): coffee_funded.withdraw() assert boa.env.get_balance(coffee_funded.address) == 0 ``` We'll also add a new test to make sure we can withdraw from the contract: ```python def test_fund_with_money(coffee_funded, account): # Arrange boa.env.set_balance(account.address, SEND_VALUE) # Act coffee_funded.fund(value=SEND_VALUE) # Assert funder = coffee_funded.funders(0) assert funder == account.address assert coffee_funded.to_amount_funded(funder) == SEND_VALUE ``` And lastly, we can update the `test_fund_with_money` test as well: ```python def test_fund_with_money(coffee_funded, account): # Arrange boa.env.set_balance(account.address, SEND_VALUE) # Act coffee_funded.fund(value=SEND_VALUE) # Assert funder = coffee_funded.funders(0) assert funder == account.address assert coffee_funded.to_amount_funded(funder) == SEND_VALUE ``` We'll make sure to import `boa` and `SEND_VALUE` in our test file. ```python from eth_utils import to_wei import boa from tests.conftest import SEND_VALUE ``` Now we can run our tests: ```bash mox test ``` We'll have to update our `conftest.py` file to add our new `SEND_VALUE` variable: ```python SEND_VALUE = to_wei(1, "ether") ``` Running our tests again will pass. Our tests now reuse code by using the `coffee_funded` fixture, which helps us avoid duplicating code in our tests and makes them more readable.
A practical guide to using Pytest Fixtures in Vyper smart contract testing. The lesson covers how to create custom fixtures to simplify repetitive test setup tasks, making your tests more concise and efficient.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar