5/5
## Pytest Fixtures We're going to cover the concept of pytest fixtures. In our `test_favorites.py`, we have repeated a line of code in every test: ```python favorites_contract = deploy_favorites() ``` This line deploys our favorites contract, which could take a long time if we had a thousand tests. To make our tests more efficient, we can use a concept called a **fixture**. Let's start by importing pytest: ```python import pytest ``` We can then create a fixture function like this: ```python @pytest.fixture def favorites_contract(): return deploy_favorites() ``` We can now pass the fixture as a parameter to our test functions: ```python def test_starting_values(favorites_contract): assert favorites_contract.retrieve() == 77 ``` Note, we no longer need to call `deploy_favorites()` in the body of the test function. Let's repeat this process for the other test functions: ```python def test_can_change_values(favorites_contract): # Act favorites_contract.store(42) # Assert assert favorites_contract.retrieve() == 42 def test_can_add_people(favorites_contract): # Arrange new_person = "Becca" favorite_number = 16 # Act favorites_contract.add_person(new_person, favorite_number) # Assert assert favorites_contract.list_of_people()[0] == (favorite_number, new_person) ``` Now, when we run `mox test`, our tests will run much faster because `deploy_favorites()` is only called once. Pytest fixtures also have a `scope` parameter, which controls the number of times the fixture is executed. The default scope is `function`. The `scope` parameter can be set to: * `function` (default): The fixture is executed once per test function. * `session`: The fixture is executed once per test session. Let's set our fixture to a session scope: ```python @pytest.fixture(scope="session") def favorites_contract(): favorites_contract = deploy_favorites() return favorites_contract ``` This will deploy the contract only once for the entire test session. If our `deploy_favorites()` function took an hour to run, we would have to wait an hour between every test. But, now that we have a session scope, our tests run much more efficiently. We can also use a `time.sleep` function to slow down a test: ```python import time def deploy_favorites(): favorites_contract = favorites.deploy() starting_number = favorites_contract.retrieve() print(f"Starting number is: {starting_number}") favorites_contract.store(77) ending_number = favorites_contract.retrieve() print(f"Ending number is: {ending_number}") time.sleep(5) return favorites_contract ``` If we run `mox test` now, we'll see that the tests are much slower: ```bash mox test ``` We can see that it takes 15.05 seconds to run all three tests because we're waiting 5 seconds between each test. Fixtures are a powerful way to make our tests more efficient and easier to write.
We're going to cover the concept of pytest fixtures.
In our test_favorites.py
, we have repeated a line of code in every test:
This line deploys our favorites contract, which could take a long time if we had a thousand tests.
To make our tests more efficient, we can use a concept called a fixture.
Let's start by importing pytest:
We can then create a fixture function like this:
We can now pass the fixture as a parameter to our test functions:
Note, we no longer need to call deploy_favorites()
in the body of the test function.
Let's repeat this process for the other test functions:
Now, when we run mox test
, our tests will run much faster because deploy_favorites()
is only called once.
Pytest fixtures also have a scope
parameter, which controls the number of times the fixture is executed. The default scope is function
.
The scope
parameter can be set to:
function
(default): The fixture is executed once per test function.
session
: The fixture is executed once per test session.
Let's set our fixture to a session scope:
This will deploy the contract only once for the entire test session.
If our deploy_favorites()
function took an hour to run, we would have to wait an hour between every test. But, now that we have a session scope, our tests run much more efficiently.
We can also use a time.sleep
function to slow down a test:
If we run mox test
now, we'll see that the tests are much slower:
We can see that it takes 15.05 seconds to run all three tests because we're waiting 5 seconds between each test.
Fixtures are a powerful way to make our tests more efficient and easier to write.
A comprehensive guide to using Pytest fixtures for efficient testing in your Python projects. This lesson explores the concept of fixtures, their different scopes (function, session), and how to utilize them to streamline your test workflow.
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