1/5
## Mid-Section Workshop We've done a little bit of refactoring to make our code more efficient. We're also starting to do some interesting things with our tests, and we want you to write one of the last tests we'll need before we move on to some more advanced topics. We're going to do Workshop 1. We want you to write a test that: * funds the contract with 10 different funders * withdraws the funds using the owner * asserts that: * the ending balance of `buy_me_a_coffee` is 0 * the ending balance of the owner is the addition of all the amounts the funders added This might sound simple, but there are some tricky parts to it. We've seen how to use `boa.env.generate_address()` to make a random address to use as a funder, so try making 10 of them. For bonus points, see if you can write some code that makes it a bit easier to make a bunch of these addresses at once, rather than just copy-pasting the code 10 times. To withdraw the funds, we'll have to use the `prank` function to pretend we are the owner. We've done this in earlier lessons, so this should be a review. Finally, we'll want to use the `boa.env.get_balance()` function to check the ending balance of both the contract and the owner. We've also seen this function in earlier lessons, so this part should be familiar. As always, spend at most 25 minutes on this workshop. If you're stuck, take a break and ask for help or look at the solution on GitHub. ```python RANDOM_USER = boa.env.generate_address("non-owner") ``` ```python def test_fund_with_money(coffee, account): # Arrange boa.env.set_balance(account.address, SEND_VALUE) # Act coffee.fund(value=SEND_VALUE) # Asset funder = coffee.funders(0) assert funder == account.address assert coffee.funder_to_amount(funder) == SEND_VALUE ``` ```python def test_owner_can_withdraw(coffee, funded): with boa.env.prank(coffee.OWNER()): coffee_funded.withdraw() assert boa.env.get_balance(coffee_funded.address) == 0 ```
A comprehensive guide to Vyper smart contract testing using Pytest - This lesson teaches you how to test a smart contract using Pytest, covering key concepts like setting up your test environment, writing individual tests, and ensuring your code is robust and secure.
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