Skip to content

Option added in conftest.py not available - leads to KeyError #416

Closed

Description

@pytestbot

Originally reported by: Jason R. Coombs (BitBucket: jaraco, GitHub: jaraco)


Using the directory tree indicated here:

.
│
└───foo
    │   conftest.py
    │
    └───unit
            test_stuff.py

(with full content attached), if I run py.test in '.', the test run fails with a KeyError:

============================= test session starts =============================
platform win32 -- Python 3.4.0 -- pytest-2.5.1
collected 1 items

foo/unit/test_stuff.py E

=================================== ERRORS ====================================
_________________________ ERROR at setup of test_slow _________________________

item = <Function 'test_slow'>

    def pytest_runtest_setup(item):
>       if 'slow' in item.keywords and not item.config.getvalue("runslow"):

foo\conftest.py:8:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_pytest.config.Config object at 0x0000000002FAFB00>, name = 'runslow'
path = None

    def getvalue(self, name, path=None):
        """ return command line option value.

            :arg name: name of the command line option

            (deprecated) if we can't find the option also lookup
            the name in a matching conftest file.
            """
        try:
            return getattr(self.option, name)
        except AttributeError:
>           return self._getconftest(name, path, check=False)

c:\python\lib\site-packages\pytest-2.5.1-py3.4.egg\_pytest\config.py:807:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_pytest.config.Config object at 0x0000000002FAFB00>, name = 'runslow'
path = None, check = False

    def _getconftest(self, name, path=None, check=False):
        if check:
            self._checkconftest(name)
>       return self._conftest.rget(name, path)

c:\python\lib\site-packages\pytest-2.5.1-py3.4.egg\_pytest\config.py:782:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_pytest.config.Conftest object at 0x00000000035DD2B0>, name = 'runslow'
path = None

    def rget(self, name, path=None):
>       mod, value = self.rget_with_confmod(name, path)

c:\python\lib\site-packages\pytest-2.5.1-py3.4.egg\_pytest\config.py:516:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_pytest.config.Conftest object at 0x00000000035DD2B0>, name = 'runslow'
path = None

    def rget_with_confmod(self, name, path=None):
        modules = self.getconftestmodules(path)
        modules.reverse()
        for mod in modules:
            try:
                return mod, getattr(mod, name)
            except AttributeError:
                continue
>       raise KeyError(name)
E       KeyError: 'runslow'

c:\python\lib\site-packages\pytest-2.5.1-py3.4.egg\_pytest\config.py:527: KeyError
=========================== 1 error in 0.08 seconds ===========================

I suspect this may be related to the note in the docs which indicates "conftest.py files from sub directories are by default not loaded at tool startup."

However, curiously, if I rename 'foo' to 'tests', the failure goes away (and the test is skipped as expected).

Oddly, this problem seemed to emerge with pytest 2.4, but only because pytest 2.3 didn't honor pytest_runtest_setup in that conftest.py.

Also, in my production project (pmxbot, the command-line option isn't added even though the plugin is located in tests/conftest.py, contrary to what I found in this example.

I'm reporting this issue primarily because the issue seems to only be present in some circumstances, which strikes me as a bug. If it's not, please do suggest the recommended approach (where should conftest exist to extend the command-line options).


Activity

  1. pytestbot commented on Dec 27, 2013

    @pytestbot
    ContributorAuthor

    Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


    Indeed, the docs are not correct because "test" and "tests" is looked up as a special case. Either we extend the logic to try conftest loading in all ./X/conftest.py or we need to clarify the docs. I favor the latter.

  2. pytestbot commented on Jan 23, 2014

    @pytestbot
    ContributorAuthor

    Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


    Duplicate of #430.

  3. pytestbot commented on Jan 31, 2014

    @pytestbot
    ContributorAuthor

    Original comment by Marc Abramowitz (BitBucket: msabramo, GitHub: msabramo):


    @jaraco: You might be interested in this issue which is in the same vein, but not exactly the same:

    https://bitbucket.org/hpk42/pytest/issue/436/command-line-arguments-that-are-files

    In general, py.tests's finding of conftest.py files is a bit tricky I'm finding.

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

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions