尝试访问命令行variab时发生Pytest键错误

2024-05-03 17:13:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个需要pytest的命令行变量的框架。这个变量叫做environment,但是当我试图访问这个变量时,我得到一个AttributeError:“module”对象没有属性“config”。在

以下是我的测试设置:

File organization

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

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

我想我遇到了一个问题,当我加载内部conftest.py我试图导入框架。当我导入框架时,它试图访问py.测试变量。这个变量,即使pytest已经在我的outer的pytest_addoption()部分看到了它-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")

内容框架.py公司名称:

^{pr2}$

内部内容conftest.py公司名称:

import pytest
from testing.framework import Environment

试验内容_样品.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.测试-q—cmdopt=类型2

我收到以下错误:

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命令行变量的外部框架?在


Tags: inpyconfighomepytestmylocalline
2条回答

理想情况下,您应该有fixture从框架中为您提供对象,例如一个用户实例。在

这些fixture可以receivefixture,cmdoptfixture,然后可以将该值传递给init方法或工厂函数。在

例如

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

然后在你的测试中

^{pr2}$

您只需将变量添加到模块中即可框架.py,将框架模块导入外部conftest.py并在cmdopt方法下设置此变量。在

代码输入框架.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公司名称:

^{pr2}$

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

相关问题 更多 >