pytest-dev / pytest-dev/pytest

fixture was not found when defined in a test file but discovered twice

Open
#6,266 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I have created a minimal case:

$ tree .
.
├── dir1
│   └── dir2 -> ../dir2
└── dir2
    └── test_fixture.py
$ cat dir2/test_fixture.py
import pytest

@pytest.fixture
def i_am_a_fixture():
    return

def test_fixture(i_am_a_fixture):
    pass
$ pytest
======================= test session starts ========================
platform linux -- Python 3.8.0, pytest-4.6.6, py-1.8.0, pluggy-0.13.1
rootdir: /tmp/test
collected 2 items

dir1/dir2/test_fixture.py .                                  [ 50%]
dir2/test_fixture.py E                                       [100%]

============================== ERRORS ==============================
__________________ ERROR at setup of test_fixture __________________
file /tmp/test/dir1/dir2/test_fixture.py, line 9
  def test_fixture(i_am_a_fixture):
E       fixture 'i_am_a_fixture' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/tmp/test/dir1/dir2/test_fixture.py:9
================ 1 passed, 1 error in 0.01 seconds =================

The error is present in latest 5.2.4 version as well.

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 reproducing the minimal symlinked-directory case from the issue with pytest and inspect how the two copies of dir2/test_fixture.py are collected. Trace fixture lookup for i_am_a_fixture in the duplicated test module. Done means both collected tests can resolve the fixture without the fixture-not-found error.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.