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

async pytest-bdd step definitions

Open mathew-jithin opened this issue 4 years ago • 7 comments

Just shooting this question with reference to https://github.com/pytest-dev/pytest-bdd/issues/223

Is there any way the same can be handled with pytest-asyncio? Hope someone will please help if you have worked on something similar.

mathew-jithin avatar Nov 02 '20 06:11 mathew-jithin

My understanding is that there are two questions in the room:

  1. Does pytest-asyncio work with pytest-bdd when using async test steps?
  2. Can pytest-asyncio be used to provide support for async step definitions in pytest-bdd?

pytest-asyncio basically works like this: If an asynchronous test case is marked with the keyword asyncio, the plugin surrounds that test case with a synchronous wrapper. The wrapper executes the async function in an event loop. When you use the @when decorator, the decorated function will be synchronous: https://github.com/pytest-dev/pytest-bdd/blob/02e667f239618e24dab6df787be43b670bcc9df8/pytest_bdd/steps.py#L105

Therefore, the decorated function cannot be executed in an event loop. In other words: No, I don't think pytest-bdd and pytest-asyncio can work together without modifications.

Here is the code that wraps the async function: https://github.com/pytest-dev/pytest-asyncio/blob/1bb7f300f4f5c31d47675ed018e9a46ecd4d8496/pytest_asyncio/plugin.py#L166-L184

But it should be possible to make both plugins work together. For example, there is already a custom integration for Hypothesis: https://github.com/pytest-dev/pytest-asyncio/blob/1bb7f300f4f5c31d47675ed018e9a46ecd4d8496/pytest_asyncio/plugin.py#L153-L157

This solution relies on markers provided by Hypothesis. We could create a similar mechanism with the following steps:

  1. pytest-bdd needs to provide metadata for BDD steps decorated with when, given, …, so that pytest-asyncio can recognize them as such
  2. pytest-asyncio recognizes the fact that a @when decorator is present and wraps the underlying async function, instead of the decorator.

I personally don't like the approach, though, as we would implement pytest-bdd support as another special case. It would be much nicer to find a more general way for pytest-asyncio to play with other plugins. But I'm also not in the position to decide :)

seifertm avatar Nov 02 '20 13:11 seifertm

Thanks @seifertm. Really appreciate the response.

Saw this PR which still remains in review in pytest-bdd. Maybe if a similar handling is done in their side the issue can be addressed better.

mathew-jithin avatar Nov 02 '20 16:11 mathew-jithin

At the time of writing pytest-asyncio is compatible with flaky and hypothesis. I think that compatibility with pytest-bdd is a reasonable feature request. @Tinche Does it make sense to label this issue accordingly? I think labels would help keeping track of triaged issues. I'm thinking of the labels feature and interop, but I'm open to other solutions.

seifertm avatar Jan 07 '22 09:01 seifertm

@seifertm Added the 'feature' label!

Tinche avatar Jan 07 '22 11:01 Tinche

Has this been done or is it still open ?

KelvinSan avatar Aug 10 '23 15:08 KelvinSan

@KelvinSan There has been no progress on this from the pytest-asyncio side.

In the meantime, the pytest-bdd issue linked by the OP received some attention. One comment presents a "comaptibility decorator" for using pytest-bdd steps with pytest-asyncio.

seifertm avatar Aug 11 '23 07:08 seifertm