pytest-dev / pytest-dev/pytest

conftest.py definitions in subdirectories shadow those in parent directories

Open
#10,851 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When there is a conftest.py in a subdirectory that contains a pytest.fixture of the same name as one in a conftest.py file in a parent directory, the fixture in the subdirectory runs for tests in the subdirectory, but the one in the parent does not.

Repro

Create a directory. Put in:

conftest.py

conftest.py

import os
import pytest

@pytest.fixture(autouse=True)
def record_file_attribute(record_property, request):
    file_path = os.path.relpath(request.node.location[0])
    record_property("file", file_path)

test_hello.py

def test_hello(record_property):
    assert True

submod/__init__.py (empty file)

submod/test_hello.py with the same contents as test_hello.py above.

submod/conftest.py with the same contents as test_hello.py above, but with "file" changed to "file2".

Run pytest in the root directory with --junitxml=output.xml. The output will be:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
  <testsuite name="pytest" errors="0" failures="0" skipped="0" tests="2" time="0.028" timestamp="2023-03-30T11:32:48.799625" hostname="fross-mbp">
    <testcase classname="test_minimal" name="test_hello" file="test_hello.py" line="0" time="0.001">
      <properties>
        <property name="file" value="test_hello.py"/>
      </properties>
    </testcase>
    <testcase classname="submod.test_hello" name="test_other" file="submod/test_hello.py" line="0" time="0.001">
      <properties>
        <property name="file2" value="submod/test_hello.py"/>
      </properties>
    </testcase>
  </testsuite>
</testsuites>

instead of having a file property on both.

pytest version 6.2.5

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 issue with the root and submod conftest.py files, their test_hello.py files, and pytest --junitxml=output.xml. Compare the generated output.xml properties for both tests, then trace pytest's fixture resolution for parent and subdirectory conftest.py files. Done means both tests retain the expected parent fixture property while the subdirectory fixture behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.