如何在pytest测试中获得fixture运行的计数



我正在DB中插入数据,并对使用行id进行测试的端点进行API调用。我有一个参数化测试,该测试多次运行fixture。

@pytest.mark.parametrize(
"endpoint",
[
"/github/access-form",
"/github/issue-form",
],
)
def test_marketplace_details(
client: TestClient, session: Session, endpoint: str, add_marketplace_product_materio_ts: MarketplaceProductLink
):
# here I want to know the id of inserted record. I guess I can get it from the count of fixture "add_marketplace_product_materio_ts" run
r = client.get(f"{endpoint}?marketplace=1")
assert r.status_code == 200
data = r.json()
assert data["marketplaces"] == IsList(
IsPartialDict(
name="themeselection",
purchase_verification_url="https://google.com",
)
)
assert data["brands"] == []
assert data["product_w_technology_name"] == []

因此,我如何获得测试中运行的fixture的计数,以便将正确的id传递给r = client.get(f"{endpoint}?marketplace=1")。这里的marketplace=11应该是夹具运行的计数。

谢谢。

您可以使用enumerate:

@pytest.mark.parametrize("idx, endpoint", enumerate(["zero", "one"]))
def test_marketplace_details(idx, endpoint):
print(idx, endpoint)
# prints:
# 0 zero
# 1 one

最新更新