DEV Community

Cover image for Testing in Python: Pytest fixtures scope
Mangabo Kolawole
Mangabo Kolawole Subscriber

Posted on β€’ Originally published at Medium

4 1

Testing in Python: Pytest fixtures scope

I was recently working on a Django project. We decided to use pytest to write the tests because we planned to have a lot of unit tests, and pytest is very and ridiculously fast.

The project uses a lot of fixtures, and we quickly ran into a problem: something I will call fixtures hell. Let me explain.

The problem

We have a fixture that uses another fixture.

import pytest

pytest.fixture
def user():
    return User.objects.create(name="foo")

pytest.fixture
def business(user):
    return Business.objects.create(name="foo", owner=user)
Enter fullscreen mode Exit fullscreen mode

Now, if we want to use the business feature in a test, it won't work and return an error.

import pytest


pytest.mark.django_db
def test_activate_business(business):
    business.activate()
Enter fullscreen mode Exit fullscreen mode

This is because the business fixture depends on the user fixture, thus, we need to add the business fixture and the user fixture. And the business fixture was also shared between multiple tests.

Solution

We decided to add the fixture in the conftest.py file. Doing this, the fixture was available on multiple tests and set the autouse to True so you don't even need to require it in your tests.

import pytest

pytest.fixture
def user():
    return User.objects.create(name="foo")

pytest.fixture(autouse=True)
def business(user):
    return Business.objects.create(name="foo", owner=user)
Enter fullscreen mode Exit fullscreen mode

Also, learning about pytest scopes can be helpful to better the organization of fixtures in your tests.

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay