如何将多个dict作为参数发送到pytest fixture



我正在尝试编写一个测试来传递有效和无效的代理详细信息。我已经编写了一个Pytest fixture,它将进行请求并返回响应。但我的问题是,我想在比赛期间发送无效和有效的代理详细信息。有人能纠正我这种方法是否正确,或者建议我使用有效的方法吗?我是Pytests的新手。我试过以下方法。

@pytest.fixture(scope="module")
@pytest.mark.parametrize("proxyDict",[
({
"http": "web-proxy.testsite:8080",
"https": "web-proxy.testsite:8080"
}),
({
"http": "web-wrong:8080",
"https": "web-.wrong:8080"
})
])
def cve_response(proxy_dict):
year="2018"
base_url = 'https://static.nvd.nist.gov/feeds/json/cve/1.0/nvdcve-1.0-' + str(year) + '.json.zip'
headers = {
"content-type": "application/json"
}
response_data = requests.request("GET", base_url, headers=headers, verify=False, stream=True,
proxies=proxy_dict)
yield response_data
@pytest.mark.proxy
def test_valid_proxy(cve_response):
assert 200 == cve_response.status_code
@pytest.mark.invalidproxy
def test_invalid_proxy(cve_response):
assert not 200 == cve_response.status_code

您需要参数化测试用例,而不是夹具。此外,这不是使用固定装置的用例。因此,以下是您应该如何处理它:

data = [{
"http": "web-proxy.testsite:8080",
"https": "web-proxy.testsite:8080"
},
{
"http": "web-wrong:8080",
"https": "web-.wrong:8080"
}]
def cve_response(proxy_dict):
year="2018"
base_url = 'https://static.nvd.nist.gov/feeds/json/cve/1.0/nvdcve-1.0-' + str(year) + '.json.zip'
headers = {
"content-type": "application/json"
}
response_data = requests.request("GET", base_url, headers=headers, verify=False, stream=True,
proxies=proxy_dict)
return response_data
@pytest.mark.proxy
@pytest.mark.parameterize("proxy", data)
def test_valid_proxy(proxy):
assert 200 == cve_response(proxy).status_code
@pytest.mark.invalidproxy
@pytest.mark.parameterize("proxy", data)
def test_invalid_proxy(proxy):
assert not 200 == cve_response(proxy).status_code

根据需求,您可以选择为正面和负面场景提供不同的数据。

最新更新