尝试访问命令行变量时的Pytest KeyError

xce*_*n27 11 python pytest python-3.x

我用一个新的更简单的假设置重新创建了这个问题.

我有一个需要来自pytest的命令lane变量的框架.这个变量叫做环境,但当我尝试访问该变量时,我得到一个AttributeError:'module'对象没有属性'config'.

这是我的测试设置:

文件组织

我知道py.test按此顺序加载:

  1. Pytest插件
  2. 外部插件
  3. conftest.py文件按顺序从外部文件到内部文件.

我想我遇到了一个问题,当我加载内部conftest.py时,我试图导入框架.当我导入框架时,它尝试访问py.test变量.这个变量,即使pytest在我的outer-conftest.py的pytest_addoption()部分中看到它,也不准备在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")
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

内部conftest.py的内容:

import pytest
from testing.framework import Environment
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我在testing /文件夹中运行以下命令: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
Run Code Online (Sandbox Code Playgroud)

有没有一种好方法可以使用依赖于pytest命令行变量的外部框架?

pfc*_*ise 2

理想情况下,您应该拥有从框架中为您提供对象的固定装置,例如用户实例。

这些固定装置可以接收固定cmdopt装置,然后可以将该值传递给 init 方法或工厂函数。

例如

@pytest.fixture(scope="function")
def user(cmdarg):
    return Users(cmdarg, ...)
Run Code Online (Sandbox Code Playgroud)

然后在你的测试中,

def test_something(user):
    # do something with user object
Run Code Online (Sandbox Code Playgroud)