Skip to content

Load initial conftest when an option value is an existing file path#14696

Open
breidenbach0 wants to merge 1 commit into
pytest-dev:mainfrom
breidenbach0:fix/conftest-not-loaded-when-option-value-is-filepath-13913
Open

Load initial conftest when an option value is an existing file path#14696
breidenbach0 wants to merge 1 commit into
pytest-dev:mainfrom
breidenbach0:fix/conftest-not-loaded-when-option-value-is-filepath-13913

Conversation

@breidenbach0

Copy link
Copy Markdown

Problem

Fixes #13913.

Since pytest 9 (commit 115b8703, switch to intermixed argument parsing), a conftest.py that registers command line options is sometimes not loaded as an initial conftest, so its options are reported as unrecognized arguments. Reported by SQLAlchemy's lead (@zzzeek) and confirmed by at least one other project.

Reproducer (minimal):

tests/conftest.py   # defines --write-idents via pytest_addoption
test_it.py
idents.txt          # an existing file
$ pytest --write-idents idents.txt
ERROR: ... unrecognized arguments: --write-idents

Reproduced on current main.

Cause

(as diagnosed by @bluetech in the issue)

With intermixed parsing, the value of an option that is a path to an existing file ends up among the positional args during pre-parsing:

file_or_dir = ['idents.txt']      # was in `unknown` before intermixed parsing
unknown     = ['--write-idents']

_set_initial_conftests() then treats idents.txt as a test-path anchor. Because it is a file:

  1. anchor.is_dir() is false → the test* sub-directory glob is skipped, and
  2. the anchor list is non-empty → the invocation-directory test* fallback is skipped too.

So the test*/conftest.py that registers --write-idents is never discovered.

Fix

Resolve a file anchor to its containing directory (_get_directory) so both the conftest loading and the test* sub-directory discovery consider the right location. This follows the fix direction @bluetech sketched in the issue.

The only behavioral change is that a file argument also triggers test* discovery relative to its directory — consistent with the existing test* auto-discovery feature (#14608) and the documented behavior; the full test suite is unaffected.

Tests

  • New regression test test_initial_conftest_loaded_when_option_value_is_existing_file (passes with the fix, fails without).
  • The pre-existing test_conftests_in_invocation_dir_tests_is_initial (pytest 9.1.0: conftest.py addoption regressed in nested directories #14608) still passes.
  • testing/test_conftest.py, test_collection.py, test_main.py, test_config.py, and a broad sweep (test_capture, test_terminal, test_session, test_runner, test_debugging, test_assertion, python/collect, python/fixtures) show no new failures versus baseline (identical pass/error counts; the only errors/failures are pre-existing and unrelated — the dev minversion/pytest.toml-parse environment issues).

News fragment changelog/13913.bugfix.rst added.

cc @bluetech @zzzeek

…ytest-dev#13913)

Since the switch to intermixed argument parsing (115b870), the *value* of a
command line option that is a path to an existing file (e.g.
``--write-idents idents.txt``) ends up among the positional arguments during
pre-parsing. _set_initial_conftests() then treated it as a test path anchor.

Because the anchor was a file, the ``test*`` sub-directory discovery was
skipped, and since the anchor list was non-empty the invocation-directory
fallback was skipped too. As a result a conftest.py that registers the option
(typically living under a ``test*`` directory) was not loaded as an initial
conftest, so the option was reported as "unrecognized".

Resolve file anchors to their containing directory (via _get_directory) so
both the conftest loading and the ``test*`` sub-directory discovery consider
the right location.

Fixes pytest-dev#13913.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytest 9 is not consistently loading conftest.py , which then fails because I'm adding custom options

1 participant