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 Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs