我用一个新的更简单的假设置重新创建了这个问题.
我有一个需要来自pytest的命令lane变量的框架.这个变量叫做环境,但当我尝试访问该变量时,我得到一个AttributeError:'module'对象没有属性'config'.
这是我的测试设置:
我知道py.test按此顺序加载:
我想我遇到了一个问题,当我加载内部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 …Run Code Online (Sandbox Code Playgroud)