Search code examples
pythontestingpytestfixtures

How to get the count of fixture run in pytest test


I am inserting the data in DB and I make an API call to the endpoint I am testing with row id. I have a parameterized test which runs the fixtures multiple times.

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

Hence, How can I get the count of fixture run in test so I can pass the correct id to r = client.get(f"{endpoint}?marketplace=1"). marketplace=1 here 1 should be count of fixture run.

Thanks.


Solution

  • You can use enumerate:

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