gpt4 book ai didi

python - 尝试访问命令行变量时出现 Pytest KeyError

转载 作者:太空狗 更新时间:2023-10-29 19:30:01 29 4
gpt4 key购买 nike

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

我有一个框架需要来自 pytest 的命令 channel 变量。此变量称为环境,但当我尝试访问该变量时,我得到一个 AttributeError:“模块”对象没有属性“配置”。

这是我的测试设置:

File organization

我知道 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")

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

最佳答案

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

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

例如

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

然后在你的测试中,

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

关于python - 尝试访问命令行变量时出现 Pytest KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995122/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com