Pytest 使用固定装置中的值进行参数化

问题描述 投票:0回答:1

我有一个 pytest 装置,它接受 URL 并返回一些数据:

@pytest.fixture
def queue_entry(url):
    return QueueEntry(url=url)

我在测试中使用httpbin。

@pytest.mark.parametrize('queue_entry', ???, indirect=True)
async def test_fetch_with_headers(self, env, httpbin, queue_entry):
    queue_entry = QueueEntry(httpbin.url + "/foo")

URL 本身来自 httpbin 固定装置。将 httpbin.url (由 httpbin 固定装置定义)传递到我的queue_entry固定装置的最佳方法是什么?

python-3.x pytest
1个回答
0
投票

固定装置可以请求其他固定装置,因此在您的情况下,queue_entry固定装置可以请求httpbin固定装置以获取由它创建的url:


import pytest

@pytest.fixture
def httpbin():
    url = "http://..."
    return url


@pytest.fixture
def queue_entry(httpbin):
    return QueueEntry(url=httpbin)

要参数化夹具中的测试,您可以查看我的以下答案:

Pytest - 从夹具参数化测试方法

© www.soinside.com 2019 - 2024. All rights reserved.