1/5
## Unit Tests We'll start with the simplest kind of test, unit tests. We'll test a function and see if it works. We can create a new folder called **unit** to hold these tests. We'll need to create a file called **conftest.py** (make sure this is in your **tests** folder, not in your **unit** folder) to hold our fixtures. ```python import pytest @pytest.fixture def coffee(): return deploy_coffee() ``` Here, we import the `pytest` library and create a function called `coffee` that returns the result of calling our deploy script. Next, we need to create a file called **test_unit_coffee.py** in our **unit** folder to write our actual tests. ```python from eth_utils import to_wei def test_price_feed_is_correct(coffee, eth_usd): assert coffee.PRICE_FEED() == eth_usd.address def test_starting_values(coffee, account): assert coffee.MINIMUM_USD() == to_wei(5, "ether") assert coffee.OWNER() == account.address ``` Here, we import the `to_wei` function from `eth_utils`, then we create two tests. The first checks if the price feed address is correct. The second tests the starting values of the coffee contract. We'll run our tests using the command: ```bash mox test ``` We'll need to make sure to import the `get_active_network` function in **conftest.py**, as well as create a fixture for our account. ```python from moccasin.config import get_active_network @pytest.fixture(scope="session") def account(): return get_active_network().get_default_account() ``` Now, we've created a `get_active_network` fixture that will always return the same default account for each test, ensuring consistency. We've also added a `to_wei` function to test the minimum USD value in Wei. If we run our tests again using the command: ```bash mox test ``` We'll see that our tests are all passing. ## TAG: add a code block to show the deployment script We've covered how to write unit tests for our coffee contract, ensuring the price feed and starting values are correct. Now, let's test the owner of the contract. Let's create a new test: ```python def test_owner_is_account(coffee, account): assert coffee.OWNER() == account.address ``` We use the `account` fixture we created in our **conftest.py** to make sure the owner of the contract is the same as the account that deployed it.
A comprehensive guide to writing unit tests for a Vyper smart contract in a Python environment. The lesson demonstrates the use of pytest fixtures to streamline testing, covering the basics of setting up a test environment, importing necessary components, and running unit tests.
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