Skip to content

Conftest file is loaded multiple times #13296

Done

Description

@wassupxP

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

Minimal reproduction example:

pytest_bug.zip

Steps to reproduce:

  1. unzip pytest_bug.zip
  2. python -m pytest .\pytest_bug\Reproducer\
    (note that running python -m pytest .\pytest_bug\reproducer\ will finish with 0 collected tests as expected)

Pip list output and OS version

OS version: Windows 11
Python Version: 3.12.9

(.venv) PS C:\venvs\reproducer\pytest_bug> python -m pip list  
Package   Version
--------- -------
colorama  0.4.6
iniconfig 2.0.0
packaging 24.2
pip       24.3.1
pluggy    1.5.0
pytest    8.3.5

Potentional fix

Resolving the path fixes the issue, but I am not sure whether it might not have impact on something else.

$ git diff
diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py
index 56b047196..39f61daaf 100644
--- a/src/_pytest/config/__init__.py
+++ b/src/_pytest/config/__init__.py
@@ -643,7 +643,7 @@ class PytestPluginManager(PluginManager):
         if self._noconftest:
             return

-        directory = self._get_directory(path)
+        directory = self._get_directory(path.resolve())


Thank you.

Activity

  1. ugomancz commented on Mar 14, 2025

    @ugomancz

    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.

  2. added
    platform: windows
    topic: config
    on Mar 21, 2025
  3. added a commit that references this issue on Nov 28, 2026
    4aab11f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform: windowstopic: config

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions