You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dear Pytest team,
I’ve encountered a bug on Windows where conftest.py is loaded multiple times if the path to the test files is specified with differing case on a case-insensitive file system. This behavior triggers the following error (on minimal reproduction example) - ValueError: option names {'--dummy-option'} already added
I think the problem might actually be in the PytestPluginManager._set_initial_conftests method.
In that method, conftest plugins are loaded from the directories provided as CLI arguments and the paths aren't being normalized, which results in loading the conftest from a path with Reproducer (wrong capital letter), which works because Windows is case-insensitive.
for initial_path in args:
path = str(initial_path)
# remove node-id syntax
i = path.find("::")
if i != -1:
path = path[:i]
anchor = absolutepath(invocation_dir / path)
# Ensure we do not break if what appears to be an anchor
# is in fact a very long option (#10169, #11394).
if safe_exists(anchor):
self._try_load_conftest(
anchor,
importmode,
rootpath,
consider_namespace_packages=consider_namespace_packages,
)
foundanchor = True
However this causes the conftest to be saved with a plugin name that doesn't correspond to its actual path later in PytestPluginManager._importconftest, which just turns the Path object to a string, preserving the wrong capital letter.
conftestpath_plugin_name = str(conftestpath)
existing = self.get_plugin(conftestpath_plugin_name)
if existing is not None:
return cast(types.ModuleType, existing)
So later, during test collection when the paths are normalized correctly, when this conftest is encountered again, self.get_plugin returns None as the conftest was previously saved with Reproducer in the conftest_plugin_name, but now it has reproducer in it.
I think the correct fix for this would be to normalize the paths to locations from CLI arguments before loading the initial conftests, by changing this line in PytestPluginManager._set_initial_conftests
diff --git a/config/__init__.py b/config/__init__.py
index 4588f4d..6ec60d0 100644
--- a/config/__init__.py
+++ b/config/__init__.py
@@ -573,7 +573,7 @@ class PytestPluginManager(PluginManager):
i = path.find("::")
if i != -1:
path = path[:i]
- anchor = absolutepath(invocation_dir / path)
+ anchor = absolutepath(invocation_dir / path).resolve()
# Ensure we do not break if what appears to be an anchor
# is in fact a very long option (#10169, #11394).
I would really appreciate it if someone more experienced could give their opinion. If this fix is correct, I can put together a pull request.
Activity
ugomancz commentedon Mar 14, 2025
I think the problem might actually be in the
PytestPluginManager._set_initial_conftestsmethod.In that method,
conftestplugins are loaded from the directories provided as CLI arguments and the paths aren't being normalized, which results in loading theconftestfrom a path withReproducer(wrong capital letter), which works because Windows is case-insensitive.However this causes the
conftestto be saved with a plugin name that doesn't correspond to its actual path later inPytestPluginManager._importconftest, which just turns thePathobject to a string, preserving the wrong capital letter.So later, during test collection when the paths are normalized correctly, when this
conftestis encountered again,self.get_pluginreturnsNoneas theconftestwas previously saved withReproducerin theconftest_plugin_name, but now it hasreproducerin it.I think the correct fix for this would be to normalize the paths to locations from CLI arguments before loading the initial conftests, by changing this line in
PytestPluginManager._set_initial_conftestsI would really appreciate it if someone more experienced could give their opinion. If this fix is correct, I can put together a pull request.
Do not import conftest.py twice for paths differing only in case