5/5
In this lesson, we're going to take a look at a different way to write tests. Typically, we code our contract, like we did in the previous lesson, and then code our tests. We also have our fixtures at the top of the file. In this lesson, we're going to do things a little differently. We're going to create a new file, called `conftest.py`, in our `tests` folder. ```python import pytest from script.deploy import deploy_favorites @pytest.fixture(scope="session") def favorites_contract(): favorites_contract = deploy_favorites() return favorites_contract ``` Now, we need to import `pytest` in the `conftest.py` file, as well as import the `deploy_favorites` function. We'll also write our fixture, like we did in the previous lesson. Next, let's go into our `test_favorites.py` file. ```python from script.deploy import deploy_favorites import pytest ``` We can now delete the fixture from `test_favorites.py`, since we now have it in the `conftest.py` file, and we can also delete the `import pytest` line. We can now run `mox test` to verify that everything passes. ```bash mox test ``` This method is just an alternative way to write and organize your tests.
In this lesson, we're going to take a look at a different way to write tests. Typically, we code our contract, like we did in the previous lesson, and then code our tests. We also have our fixtures at the top of the file. In this lesson, we're going to do things a little differently.
We're going to create a new file, called conftest.py
, in our tests
folder.
Now, we need to import pytest
in the conftest.py
file, as well as import the deploy_favorites
function. We'll also write our fixture, like we did in the previous lesson.
Next, let's go into our test_favorites.py
file.
We can now delete the fixture from test_favorites.py
, since we now have it in the conftest.py
file, and we can also delete the import pytest
line.
We can now run mox test
to verify that everything passes.
This method is just an alternative way to write and organize your tests.
A simple guide to sharing fixtures with Pytest - This lesson explains how to utilize conftests.py to share fixtures, hooks, and configurations across multiple Pytest tests, ensuring consistent testing practices.
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