How send a config variable to a py.test test?

◇◆丶佛笑我妖孽 提交于 2019-12-07 07:01:18

问题


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':

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!