Python 如何将配置变量发送到py.test测试?
我有一个测试套件,需要运行多个后端。但这不是一个简单的参数化测试,因为它适用于整个套件(多个文件/模块)。我可以通过环境控制运行,但我想知道py.test是否有更清晰的方法来表达这一点 也就是说,我在寻找这样的东西:Python 如何将配置变量发送到py.test测试?,python,unit-testing,pytest,Python,Unit Testing,Pytest,我有一个测试套件,需要运行多个后端。但这不是一个简单的参数化测试,因为它适用于整个套件(多个文件/模块)。我可以通过环境控制运行,但我想知道py.test是否有更清晰的方法来表达这一点 也就是说,我在寻找这样的东西: py.test --set-mode ALPHA 然后在我的测试中,我会读取这个值: if py.test.mode == 'ALPHA': 使用: 测试blah.py def test_something(mode): if mode == 'ALPHA':
py.test --set-mode ALPHA
然后在我的测试中,我会读取这个值:
if py.test.mode == 'ALPHA':
使用:
测试blah.py
def test_something(mode):
if mode == 'ALPHA':
assert True
else:
assert False
import pytest
def pytest_addoption(parser):
parser.addoption("--set-mode", action="store", default="ALPHA",
help="ALPHA or BETA")
@pytest.fixture
def mode(request):
return request.config.getoption("--set-mode")
conftest.py
def test_something(mode):
if mode == 'ALPHA':
assert True
else:
assert False
import pytest
def pytest_addoption(parser):
parser.addoption("--set-mode", action="store", default="ALPHA",
help="ALPHA or BETA")
@pytest.fixture
def mode(request):
return request.config.getoption("--set-mode")
看