pytest-dev / pytest-asyncio

Asyncio support for pytest
https://pytest-asyncio.readthedocs.io
Apache License 2.0
1.43k stars 151 forks source link

async pytest-bdd step definitions #195

Open mathew-jithin opened 4 years ago

mathew-jithin commented 4 years ago

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.

seifertm commented 4 years ago

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 :)

mathew-jithin commented 4 years ago

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.

seifertm commented 2 years ago

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.

Tinche commented 2 years ago

@seifertm Added the 'feature' label!

KelvinSan commented 1 year ago

Has this been done or is it still open ?

seifertm commented 1 year ago

@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.

yazeedalrubyli commented 3 months ago

Needed.