pytest-dev / pytest-dev/pytest

Support per-request scope for fixtures

Open
#456 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Originally reported by: BitBucket: brettatoms, GitHub: brettatoms


I'm not sure how to really describe this but here's a code sample that demonstrates the issue. Basically if I create a test that takes two fixtures as parameters and one of those fixtures depends on the other fixture then the fixture only gets called once and the test method has the same instance for both fixtures.

import sys
import pytest

class User:
    def __init__(self, username, is_admin=False):
        self.username = username
        self.is_admin = is_admin

@pytest.fixture
def user():
    return User('me')

@pytest.fixture
def admin_user(user):
    user.username = 'admin'
    user.is_admin = True
    return user

def test_me(user, admin_user):
    print('repr(user): ', repr(user))
    print('repr(admin_user): ', repr(admin_user))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the fixture example in the issue and tracing pytest's fixture dependency and scope handling; no source files or tests are named in the report. Done means a test with user and admin_user receives separate per-request instances while preserving fixture dependency behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.