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

时间:2015-10-07 14:35:31

标签: python python-3.x pytest

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

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

这是我的测试设置:

File organization

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

  1. Pytest插件
  2. 外部插件
  3. conftest.py文件按顺序从外部文件到内部文件。
  4. 我想我遇到了一个问题,当我加载内部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")
    

    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
    

    我在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
    

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

2 个答案:

答案 0 :(得分:0)

理想情况下,您可以使用固定装置为您提供框架中的对象,例如用户实例。

这些灯具可以receive cmdopt灯具,然后可以将该值传递给init方法或工厂函数。

e.g。

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

然后在你的测试中,

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

答案 1 :(得分:0)

您可以只将变量添加到framework.py模块,将框架模块导入到外部conftest.py,然后在cmdopt方法下设置此变量。

framework.py中的代码:

import pytest

cmdopt = None

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中的代码:

# content of conftest.py
import pytest
from . import framework

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


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

您可以做的第二件事是将变量添加到Environment类并对其进行操作。如果在获取或设置此变量时需要更多逻辑,则可以始终在Environment类中使用@property和setter。