pytest-dev / pytest-dev/pytest

A fixture with autouse set to False will always be used if it overrides a fixture with autouse set to True

Open
#3,225 2 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

  • Include a detailed description of the bug or suggestion
    I took a look at pytest's source code and it looks like pytest records the names of all fixtures with autouse set to True. It then uses this list to determine which fixtures to use, even if the overriding fixture has autouse set to False.
  • pytest and operating system versions
    platform linux2 -- Python 2.7.13, pytest-3.4.0, py-1.5.2, pluggy-0.6.0
  • Minimal example if possible
    conftest.py
@pytest.fixture(autouse=True)
def foo():
    pass

test_me.py:

@pytest.fixture()
def foo():
    assert False

def test_bar(foo):
    pass

def test_baz():
    pass

Returns the following error:

____________________________________________________________________________ ERROR at setup of test_bar _____________________________________________________________________________

    @pytest.fixture()
    def foo():
>       assert False
E       assert False

____________________________________________________________________________ ERROR at setup of test_baz _____________________________________________________________________________

    @pytest.fixture()
    def foo():
>       assert False
E       assert False

I expect only test_bar to fail. Setting autouse to False for foo in conftest.py correctly causes only test_bar to fail.

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

Reproduce the fixture interaction using conftest.py and test_me.py with pytest's stated versions, then trace how autouse fixture names and overriding fixtures are selected. Confirm the fix by ensuring test_bar fails through its explicit fixture while test_baz does not use the overridden fixture.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.