pytest-trio
pytest-trio copied to clipboard
Async fixtures do not support `autouse=True`
Async fixtures defined with autouse aren't automatically used. E.g.
@pytest.fixture(autouse=True):
async def clear_some_db_table():
...
I'm sure this is a side-effect of the trio-fixture workaround approach explained in the docs, but it still might be possible to find those fixtures and make them work. If not, I think this rub is at least worth specifically mentioning in the docs.
I think the issue is that when we scan through to find and run all the trio fixtures, we do that by scanning over the test's kwargs, and I guess autouse fixtures don't show up in kwargs? The relevant code is in plugin.py, _trio_test_runner_factory which delegates to register_and_collect_dependencies. It's probably easy to fix -- we just need to figure out some way to query pytest for the full list of fixtures, not just the ones that get passed into the test function. Likely it's stashed on the item object somewhere? That's this magical pytest thing that has all kinds of undocumented attributes on it.
This does work with pytest-anyio, so we can likely just copy over the relevant code from over there.