Skip to content

Standard Mocking Patterns for Native SQL query executions + Fast API with Async Calls #603

Answered by seifertm
sarathsund asked this question in Q&A
Discussion options

You must be logged in to vote

My understanding is that you want to test calls inside your run method, for example that cursor.execute is called with the correct sql argument.

Pytest-asyncio merely offers setup and teardown of asyncio event loops. It doesn't offer anything that will make mocking easier. Nevertheless, I'll first try to answer your question specifically and then answer more broadly.

Specific answer

It's possible to use mock.AsyncMock from the standard library to mock coroutines and the like.

In your case, you'd have to create a Mock that returns a mocked connection when __aenter__ is invoked (i.e. when the async with context is entered). The mock object returned by __aenter__ represents your connection v…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by sarathsund
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants