Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support async tests which use Hypothesis #102

Merged
merged 2 commits into from
Dec 26, 2018
Merged

Conversation

Zac-HD
Copy link
Member

@Zac-HD Zac-HD commented Dec 12, 2018

Closes #93.

@Zac-HD Zac-HD force-pushed the hypothesis branch 2 times, most recently from 39c1f9d to dc51d45 Compare December 12, 2018 08:39
pytest_asyncio/plugin.py Outdated Show resolved Hide resolved

@functools.wraps(func)
def inner(**kwargs):
loop = asyncio.get_event_loop_policy().new_event_loop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not bare asyncio.new_event_loop() here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest-asyncio works slightly different with event loops:

  1. It installs the loop as default
  2. Restores previously used default loop after the test finishing

Does your change follow this behavior?

Copy link
Member Author

@Zac-HD Zac-HD Dec 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not bare asyncio.new_event_loop() here?

There are so many ways to get an event loop that I just copied the event_loop fixture; I assume there's a reason it was chosen there.

Does your change [set and restore the default loop]?

Async fixtures are not supported with Hypothesis tests, so there is no way to follow this behaviour. However this is only observable when the event_loop fixture has been overridden.

@Zac-HD
Copy link
Member Author

Zac-HD commented Dec 14, 2018

@asvetlov is there anything else? @Tinche do you have any comments?

@Tinche
Copy link
Member

Tinche commented Dec 16, 2018

I think this is looking good! Looking forward to merging it in, as an avid user of Hypothesis :) Thank you for your contribution.

Gonna leave it open for a few days to solicit additional comments.

How difficult would it be in the future to add support for:

  • fixtures using Hypothesis
  • Hypothesis state machines using async steps?

Having those as well would really rock.

@Zac-HD
Copy link
Member Author

Zac-HD commented Dec 17, 2018

Awesome, glad I could help! Hopefully it will be released to users before the holidays 🎄:santa:


Sync fixtures already work, with the notable caveat (HypothesisWorks/hypothesis#377) that function-scoped fixtures are only set up once for all test cases. Automatically converting function-scoped to 'test case scoped' fixtures is possible but nontrivial. Once that's done, supporting async function-scoped fixtures would be a few hours work plumbing it all together correctly.

Async stateful testing would be obviously hugely valuable, but there's a catch: to make it repeatable (and powerful), Hypothesis would have to control the scheduling of tasks on the event loop. This is certainly possible, but out of scope for Hypothesis itself and I don't know enough about the asyncio internals to do it - though I'd be happy to consult on the Hypothesis side! See e.g. HypothesisWorks/hypothesis#1693 and links; there's also a prototype hypothesis-trio package based on similar conversations when working on pytest-trio but I don't know how it's gone.

TLDR: both are difficult; not impossible but not priorities for me either.

@Tinche
Copy link
Member

Tinche commented Dec 26, 2018

Thanks for your work on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants