python-trio / pytest-trio

Pytest plugin for trio
Other
54 stars 25 forks source link

Async fixtures do not support `autouse=True` #123

Open mikenerone opened 3 years ago

mikenerone commented 3 years ago

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.

njsmith commented 3 years ago

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.

jakkdl commented 1 week ago

This does work with pytest-anyio, so we can likely just copy over the relevant code from over there.