python - 如何在 Pytest 中使用 fixture 的覆盖参数?

标签 python python-3.x pytest fixtures

假设我有一个这样的参数化 fixture :

@pytest.fixture(params=[1, 2, 800]):
def resource(request):
    return Resource(capacity=request.param)

当我在测试函数中使用 fixture 作为参数时,Pytest 对所有三个版本运行测试:

def test_resource(resource):  # Runs for capacities 1, 2, and 800.
    assert resource.is_okay()

但是,对于某些测试,我想更改构建 fixture 的参数:

def test_worker(resource, worker):  # Please run this for capacities 1 and 5.
    worker.use(resource)
    assert worker.is_okay()

我如何指定只接收特定版本的指定 fixture ?

最佳答案

如果您想对不同的测试使用不同的参数集,那么 pytest.mark.parametrize 会很有帮助。

@pytest.mark.parametrize("resource", [1, 2, 800], indirect=True)
def test_resource(resource):
    assert resource.is_okay()

@pytest.mark.parametrize("resource", [1, 5], indirect=True)
def test_resource_other(resource):
    assert resource.is_okay()

关于python - 如何在 Pytest 中使用 fixture 的覆盖参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069551/

相关文章:

php - 如何将 Python 加密转换为 PHP?

python - Pycharm 退出代码 0

python - 机器人只需要一个命令

Python 3.5.1 - 将同一输入行上的多个整数读取到列表中

python - 用Python读取一个excel文件,在不改变样式的情况下进行修改

Python 请求获取的参数是 ISO 日期时间不起作用

python - 根据另一个 fixture 的值对值列表进行参数化

python - 如何模拟包含库中的变量

python - 在pytest中首先执行哪个文件?

python - 将 win32com 与多线程一起使用