pytest-dev / pytest-dev/pytest

Override a fixture on a test module level:with "name" key_word_arg, its may not Override

Open
#12,952 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

description

I have some fixtures in the same test file, and I need to reload one of them as following:

@pytest.fixture()
def f1():
    return 1

@pytest.fixture(name="f1")
def f0():
    return 0
what i want

When i call “f1” its return 0 because its actually executed “f0” as following:

class TestLib:
    def test_1(self, f1):
        assert f1 == 0
pytest result

But this is fail for test

________________________________ TestLib.test_1 ________________________________

self = <lib.test.test_lib.TestLib object at 0x104b3df40>, f1 = 1

    def test_1(self, f1):
>       assert f1 == 0
E       assert 1 == 0

lib/test/test_lib.py:21: AssertionError
=========================== short test summary info ============================
FAILED lib/test/test_lib.py::TestLib::test_1 - assert 1 == 0
env

os:mac 15.0.1
python:3.12.4
pytest:8.3.2

more detail

When I change name ‘f0’ -> 'f2', as following, it works well:

@pytest.fixture()
def f1():
    return 1


@pytest.fixture(name="f1")
def f2():
    return 2

So I summarized that the overloading of fixtures in the same file is not in the order of bytecode compilation, but in the lexicographic order of functions, resulting in the phenomenon where f0 cannot reload f1 while f2 can reload f1.

I'm not sure if this is a feature or a bug

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 with the reproduction described in lib/test/test_lib.py, comparing the two fixture definitions that use name="f1" and the f0/f2 function names. Trace pytest's fixture registration and name-resolution behavior, then add regression coverage showing whether the later definition should override the earlier one and run the focused test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.