I have a test suite which needs to run with multiple backends. It isn't a simple parameterized test though since it applies to the whole suite (multiple files/modules). I can control the run via the environment, but I'm wondering if py.test has a clearer way to express this.
That is, I'm looking for something like this:
py.test --set-mode ALPHA
Then in my test I would read this value:
if py.test.mode == 'ALPHA':
Using pytest_addoption
:
test_blah.py
def test_something(mode):
if mode == 'ALPHA':
assert True
else:
assert False
conftest.py
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")
See Pass different values to a test function, depending on command line options.
来源:https://stackoverflow.com/questions/21329782/how-send-a-config-variable-to-a-py-test-test