如何将配置变量发送到 py.test 测试? [英] How send a config variable to a py.test test?

查看:72
本文介绍了如何将配置变量发送到 py.test 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在多个后端运行的测试套件.这不是一个简单的参数化测试,因为它适用于整个套件(多个文件/模块).我可以通过环境控制运行,但我想知道py.test是否有更清晰的表达方式.

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

推荐答案

使用 <代码>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")

参见 根据命令行选项将不同的值传递给测试函数.

这篇关于如何将配置变量发送到 py.test 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆