在pytest skip-if条件下使用命令行选项
问题内容:
长话短说,如果会话是针对我们的生产API运行的,我希望能够跳过一些测试。使用命令行选项设置运行测试的环境。
我遇到了使用pytest_namespace
跟踪全局变量的想法,因此在conftest.py文件中进行了设置。
def pytest_namespace():
return {'global_env': ''}
我采用命令行选项,并在conftest.py的固定装置中设置各种API URL(来自config.ini文件)。
@pytest.fixture(scope='session', autouse=True)
def configInfo(pytestconfig):
global data
environment = pytestconfig.getoption('--ENV')
print(environment)
environment = str.lower(environment)
pytest.global_env = environment
config = configparser.ConfigParser()
config.read('config.ini') # local config file
configData = config['QA-CONFIG']
if environment == 'qa':
configData = config['QA-CONFIG']
if environment == 'prod':
configData = config['PROD-CONFIG']
(...)
然后,我有了要跳过的测试,它的装饰如下:
@pytest.mark.skipif(pytest.global_env in 'prod',
reason="feature not in Prod yet")
但是,每当我针对产品运行测试时,它们都不会被跳过。我做了些摆弄,发现:
a)可以通过另一个装置访问global_env变量
@pytest.fixture(scope="session", autouse=True)
def mod_header(request):
log.info('\n-----\n| '+pytest.global_env+' |\n-----\n')
在我的日志中正确显示
b)可以在测试中访问global_env变量,正确记录环境。
c)pytest_namespace
已弃用
因此,我假设这与skipif访问该global_env的时间和夹具在测试会话中进行的时间有关。我也发现使用不推荐使用的功能是不理想的。
我的问题是:
- 如何从pytest命令行选项中获取一个值到一个skipif中?
- 有比尝试更好的方法
pytest_namespace
吗?
问题答案:
看起来按照命令行选项控制测试跳过的真正方法是将测试标记为动态 跳过 :
-
使用 pytest_addoption* 钩子添加 选项, 如下所示: *
def pytest_addoption(parser):
parser.addoption(
“–runslow”, action=”store_true”, default=False, help=”run slow tests”
) -
使用 pytest_collection_modifyitems 钩子添加标记,如下所示:
def pytest_collection_modifyitems(config, items):
if config.getoption(“–runslow”):
# –runslow given in cli: do not skip slow tests
return
skip_slow = pytest.mark.skip(reason=”need –runslow option to run”)
for item in items:
if “slow” in item.keywords:
item.add_marker(skip_slow) -
在测试中添加标记:
@pytest.mark.slow
def test_func_slow():
pass
例如,如果您想在测试中使用CLI中的数据,则使用它的 凭据
,足以在从pytestconfig检索数据时指定跳过选项:
-
使用 pytest_addoption* 钩子添加 选项, 如下所示: *
def pytest_addoption(parser):
parser.addoption(
“–credentials”,
action=”store”,
default=None,
help=”credentials to …”
) -
从pytestconfig获取它时使用 跳过 选项
@pytest.fixture(scope=”session”)
def super_secret_fixture(pytestconfig):
credentials = pytestconfig.getoption(‘–credentials’, skip=True)
… -
在测试中照常使用夹具:
def test_with_fixture(super_secret_fixture):
…
在这种情况下,您会收到类似这样的内容,即您不向--credentials
CLI发送选项:
Skipped: no 'credentials' option found
最好使用_pytest.config.get_config而不是不推荐使用的
pytest.config
如果您仍然不愿意像这样使用pytest.mark.skipif:
@pytest.mark.skipif(not _pytest.config.get_config().getoption('--credentials'), reason="--credentials was not specified")