pytest-asyncio icon indicating copy to clipboard operation
pytest-asyncio copied to clipboard

[regression] *autouse* async generator fixtures no longer executed since 0.18.0

Open RemiCardona opened this issue 2 years ago • 1 comments

Autouse async generator fixtures are no longer executed since 0.18.0, git bisect points to d8efa640f0aa1ba8856b908ba486150588018209. Fixtures with explicit dependencies work just fine.

I have tried both:

  • @pytest.fixture
  • @pytest_asyncio.fixture

to no avail. It didn't matter if the async gen fixture was a stand alone function, a method on a regular class or a method on a unittest.TestCase-derived class. None worked.

In the end, I had to do stuff like this:

     @pytest.fixture(scope='function', autouse=True)
-    async def inject_can(self, can_factory):
+    def inject_can(self, event_loop, can_factory):
         self.can = can_factory()
-        async with self.can:
-            yield
+        event_loop.run_until_complete(self.can.__aenter__())
+        yield
+        event_loop.run_until_complete(self.can.__aexit__(None, None, None))

Which kind of defeats the purpose of having async-aware stuff in the first place.

Looking at the offending commit found by bisect, I see that the main change is the removal of the wrapping that used to be done at https://github.com/pytest-dev/pytest-asyncio/commit/d8efa640f0aa1ba8856b908ba486150588018209#.

A similar wrapping is supposed to be happening here https://github.com/pytest-dev/pytest-asyncio/commit/d8efa640f0aa1ba8856b908ba486150588018209#diff-b6eecb9684dfb3a8e0ad8e43510999af4dc5dc5d03edaafd7fe6f872b22fe857R240 but that never gets called. It seems the 2 hooks are called at different times (in the pytest "lifecycle") and some filtering is done in between the 2. I tried digging in pytest's funcmanage plugin but ran out of gin.

Is there any way to fix this? Does pytest need to not filter out asyncgen fixtures even if it can't run them? (just like async tests really, it now knows about them even though it can't run them on its own)

Cheers

RemiCardona avatar May 24 '22 11:05 RemiCardona

Thank you for the thorough investigation. I will not have time to look into this for the next two weeks. If nobody else is going to pick this up, it'll have to wait until then.

seifertm avatar Jun 04 '22 06:06 seifertm

I cannot reproduce any issue with autouse async generator fixtures. I tried with Python 3.10 and pytest-asyncio v0.18.3 as well as pytest-asyncio v0.19.0.

@RemiCardona Can you provide an example that reproduces the issue?

seifertm avatar Sep 16 '22 08:09 seifertm

Since I cannot reproduce the behaviour and the OP has not responded in the last three weeks, I'll close the issue.

@RemiCardona feel free to reopen this issue, if you still run into problems.

seifertm avatar Oct 08 '22 09:10 seifertm