Skip to content

Commit b61f588

Browse files
bluetechpatchback[bot]
authored andcommitted
Merge pull request #14622 from chrisburr/fix-14608-initial-conftest-test-subdir
fix: load test* subdir conftests when initial path is missing (cherry picked from commit b375b79)
1 parent 9a567e0 commit b61f588

4 files changed

Lines changed: 34 additions & 9 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Charles Machalow
8989
Charles-Meldhine Madi Mnemoi (cmnemoi)
9090
Charnjit SiNGH (CCSJ)
9191
Cheuk Ting Ho
92+
Chris Burr
9293
Chris Mahoney
9394
Chris Lamb
9495
Chris NeJame

changelog/14608.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a regression in pytest 9.1.0 where ``conftest.py`` files located in ``<invocation dir>/test*`` were no longer loaded as initial conftests when invoked without arguments.
2+
This could cause certain hooks (like :hook:`pytest_addoption`) in these files to not fire.

src/_pytest/config/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -645,18 +645,18 @@ def _set_initial_conftests(
645645
if i != -1:
646646
path = path[:i]
647647
anchor = absolutepath(invocation_dir / path)
648-
649648
# Ensure we do not break if what appears to be an anchor
650649
# is in fact a very long option (#10169, #11394).
651-
if safe_exists(anchor):
652-
anchors.append(anchor)
653-
# Let's also consider test* subdirs.
654-
if anchor.is_dir():
655-
for x in anchor.glob("test*"):
656-
if x.is_dir():
657-
anchors.append(x)
650+
if not safe_exists(anchor):
651+
continue
652+
653+
anchors.append(anchor)
654+
# Let's also consider test* subdirs.
655+
if anchor.is_dir():
656+
anchors.extend(x for x in anchor.glob("test*") if x.is_dir())
658657
if not anchors:
659-
anchors = [invocation_dir]
658+
anchors.append(invocation_dir)
659+
anchors.extend(x for x in invocation_dir.glob("test*") if x.is_dir())
660660

661661
for anchor in anchors:
662662
self._loadconftestmodules(

testing/test_conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,28 @@ def pytest_addoption(parser):
440440
result.stdout.fnmatch_lines(["*--xyz*"])
441441

442442

443+
def test_conftests_in_invocation_dir_tests_is_initial(pytester: Pytester) -> None:
444+
"""An option registered in a conftest under ``test*`` subdir of the
445+
invocation dir is loaded as initial when no command-line arguments
446+
or `testpaths` are given (#14608).
447+
"""
448+
pytester.makepyfile(
449+
**{
450+
"tests/conftest.py": """
451+
def pytest_addoption(parser):
452+
parser.addoption("--db-url")
453+
""",
454+
"test_it.py": """
455+
def test_it(request):
456+
assert request.config.getoption("--db-url") == "scheme://host/db"
457+
""",
458+
}
459+
)
460+
result = pytester.runpytest("--db-url", "scheme://host/db")
461+
assert result.ret == ExitCode.OK
462+
result.assert_outcomes(passed=1)
463+
464+
443465
def test_conftest_import_order(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
444466
ct1 = pytester.makeconftest("")
445467
sub = pytester.mkdir("sub")

0 commit comments

Comments
 (0)