pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.13k stars 2.68k forks source link

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

Open steadyfirmness opened 5 days ago

steadyfirmness commented 5 days ago

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

RonnyPfannschmidt commented 4 days ago

We should error on such conflicts