Pytest KeyError when trying to access a command line variable

I again created the question using a new, simpler fake setup.

I have a framework that needs a command line variable from pytest. This variable is called the environment, but when I try to access this variable, I get an AttributeError object: 'module' does not have the 'config' attribute.

Here is my test setup:

File organization

I know py.test loading in the following order:

  • Pytest Plugins
  • External plugins
  • conftest.py files are in order from external file to internal file.

, , conftest.py. . , py.test. , , pytest pytest_addoption() external-conftest.py, pytest.

:

# content of conftest.py
import pytest

def pytest_addoption(parser):
    print("First")
    parser.addoption("--cmdopt", action="store", default="type1",
        help="my option: type1 or type2")


@pytest.fixture
def cmdopt(request):
    return request.config.getoption("cmdopt")

framework.py:

import pytest


class Environment:

    @staticmethod
    def env():
        '''Determine which environment we are operating in,
        if it fails - we assume dca
        '''
        return pytest.config.getoption('cmdopt')


class Users:
    __pool = Environment.env()

conftest.py:

import pytest
from testing.framework import Environment

test_sample.py:

# content of test_sample.py
def test_answer(cmdopt):
    if cmdopt == "type1":
        print ("first")
    elif cmdopt == "type2":
        print ("second")
    assert 0 # to see what was printed

: py.test -q --cmdopt = type2

:

First
Second
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 513, in getconftestmodules
    return self._path2confmods[path]
KeyError: local('/home/damonp/Repos/stuff/<my-name-redacted>/testing/tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 537, in importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('/home/damonp/Repos/stuff/<my-name-redacted>/testing/tests/conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 543, in importconftest
    mod = conftestpath.pyimport()
  File "/usr/local/lib/python3.4/dist-packages/py/_path/local.py", line 641, in pyimport
    __import__(modname)
  File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/tests/conftest.py", line 5, in <module>
    from testing.framework import Environment
  File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/framework.py", line 14, in <module>
    class Users:
  File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/framework.py", line 15, in Users
    __pool = Environment.env()
  File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/framework.py", line 11, in env
    return pytest.config.getoption('cmdopt')
AttributeError: 'module' object has no attribute 'config'
ERROR: could not load /home/damonp/Repos/stuff/<my-name-redacted>/testing/tests/conftest.py

, pytest?

+4
1

, , .

cmdopt init factory.

.

@pytest.fixture(scope="function")
def user(cmdarg):
    return Users(cmdarg, ...)

def test_something(user):
    # do something with user object
0

Source: https://habr.com/ru/post/1610595/


All Articles