5/5
## Introduction to Mocassin Testing We have our contract. What we haven't done is test it. Now, testing is probably one of the most important things any smart contract developer needs to get good at. We're going to learn how to write a test for this. We'll be using pytest, which is a testing framework built into Mocassin. It's a very well-known and widely used framework, so if you're already familiar with pytest, you're already familiar with the basics of Mocassin testing. First, we need to create a new file called `test_favorites.py`. Inside the file, we can create our first test. ```python def test_starting_values(): pass ``` We're just going to use the `pass` keyword for now to get this set up. In pytest, we look for a `test` keyword at the beginning of function names, and the functions need to be inside a folder called `tests`. So you must have a folder named `tests` and each one of your tests must start with `test` to be recognized. We'll create a test that checks to make sure our contract starts with the correct initial value, which is 7. To do this, we'll import our contract using the following code: ```python from src import favorites ``` Then, we'll deploy the contract: ```python favorites_contract = favorites.deploy() ``` Now, we'll assert that the retrieved value is 7: ```python assert favorites_contract.retrieve() == 7 ``` Now, we can test this code by opening our terminal and running the following command: ```bash mox test ``` This will run all of our tests. In this case, we have only one test, so you'll see a green dot in the terminal to indicate that the test has passed. Let's create another test. We'll copy and paste our first test and rename it to `test_starting_values_two`. Now, we can run the tests again. This time, we'll see two green dots in the terminal, indicating that both tests have passed.
We have our contract. What we haven't done is test it. Now, testing is probably one of the most important things any smart contract developer needs to get good at.
We're going to learn how to write a test for this. We'll be using pytest, which is a testing framework built into Mocassin. It's a very well-known and widely used framework, so if you're already familiar with pytest, you're already familiar with the basics of Mocassin testing.
First, we need to create a new file called test_favorites.py
. Inside the file, we can create our first test.
We're just going to use the pass
keyword for now to get this set up.
In pytest, we look for a test
keyword at the beginning of function names, and the functions need to be inside a folder called tests
. So you must have a folder named tests
and each one of your tests must start with test
to be recognized.
We'll create a test that checks to make sure our contract starts with the correct initial value, which is 7.
To do this, we'll import our contract using the following code:
Then, we'll deploy the contract:
Now, we'll assert that the retrieved value is 7:
Now, we can test this code by opening our terminal and running the following command:
This will run all of our tests. In this case, we have only one test, so you'll see a green dot in the terminal to indicate that the test has passed.
Let's create another test. We'll copy and paste our first test and rename it to test_starting_values_two
.
Now, we can run the tests again. This time, we'll see two green dots in the terminal, indicating that both tests have passed.
A simple introduction to writing Mocassin tests using Pytest. The lesson covers the basics of setting up your testing environment, including creating a new file, running basic tests, and using the 'assert' function.
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 June 10, 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 June 10, 2025