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

How to get the count of fixture run in pytest test

我将数据插入到数据库中,然后 API 调用我正在使用行 ID 测试的端点。我有一个参数化测试,它 运行 多次使用固定装置。

@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"] == []

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

的计数

谢谢。

您可以使用 enumerate:

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

# prints:
# 0 zero
# 1 one