I have an async function which computes a list of urls asynchronously, and I want to use parameterize to generate an async test for each of those urls , which will assert the status code. What I'm trying to do is something like this:
import pytest
@pytest.fixture
async def compute_urls():
urls = await compute_urls_helper()
return urls
@pytest.mark.asyncio
@pytest.mark.parameterize('url',await compute_urls())
async def test_url(compute_urls,url):
resp = await get_url(url)
assert resp.status_code == 200
I know that using 'await' inside the parameterize is not possible, so I would love to hear suggestions for this kind of operation.
You can use asyncio.run to create an event loop just to compute the params:
However I wouldn't recommend to use this method frequently, because as stated in the docs:
Therefore, you can create a session scoped fixture so that you can reuse a fixture containing each url object like so: