-
Notifications
You must be signed in to change notification settings - Fork 113
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
Dealing with async functions #155
Comments
I've not done any async work myself, so I can't make any suggestions right now. Do we think there might be a need for some async specific matchers? What might they look like, from a test's point of view? |
I need to explore this a bit more myself but I wanted to get some ideas from what others might be doing already. Having async specific (or rather future specific) matchers could makes sense based a bit on the b option above. Trying to make hamcrest deal with async methods and running a asyncio loop is just a can of worms not worth opening. Consider having one or two matchers that works on I imagine it could look something like
Wiich would make mypy/pyright able to reason about this calls a bit. The biggest immediate gain would be from introducing some warning or specific error message when |
what you're describing makes sense to me, at least in principle. I would want to see examples in the pull request, but I can easily see merging it. if possible make sure that there is clear usage demonstrated in the PR. |
WIP: Based on raises matcher but adapted to deal with future objects. Example of use ``` assert_that( await resolved(raise_exception()), future_exception(AssertionError)) ) ``` The resolved helper is used to create resolved future objects in async code. It takes a "future like" object and waits for it to complete. Ref hamcrest#155
WIP: Based on raises matcher but adapted to deal with future objects. Example of use ``` assert_that( await resolved(raise_exception()), future_exception(AssertionError)) ) ``` The resolved helper is used to create resolved future objects in async code. It takes a "future like" object and waits for it to complete. Ref hamcrest#155
Example of use ``` assert_that( await resolved(raise_exception()), future_raising(AssertionError)) ) ``` The resolved helper is used to create resolved future objects in async code. It takes a "future like" object and waits for it to complete. Ref hamcrest#155
Example of use ``` assert_that( await resolved(raise_exception()), future_raising(AssertionError)) ) ``` The resolved helper is used to create resolved future objects in async code. It takes a "future like" object and waits for it to complete. Ref hamcrest#155
When there is a async function we want to test
naïvely we may write (spoiler alert this will not work)
As a first suggestion it would be nice if the error message could guide me in the right direction like
There's a couple of ways of testing this function that does work but they look a bit clunky or have other issues. Does anyone have suggestions on how to better to do this?
a) using run_until_complete
Looks pretty neat but has the big down side it's not possible to use when the test method itself is async (like when using pytest-async)
b) testing the
result
function of a future insteadThis is arguably the more sane approach but with the assert line only being
calling(future.result)
it loses some context.The text was updated successfully, but these errors were encountered: