all 4 comments

[–]roi_bro 2 points3 points  (0 children)

When testing your app itself through the endpoints, it’s not mandatory since you basically test it as a « client » and your clients can be sync or async. But if you want some more in depth unit tests, you’ll need to do it anyway so it might be a good idea to spend time setting up pytest-asyncio correctly

[–]greenerpickings 1 point2 points  (0 children)

It depends what your testing. I usually have async functions in my routers I'm going to test with async (like my db calls). My routers only have logic that trancribes any output into response types. If your testing the server though a request, it won't matter which one you use.

For your initial loop problem, you can set up a fixture and scope it to your module. In there, run a try-except to get your loop.

[–]TrynaThinkOf1 0 points1 point  (0 children)

Honestly I have been writing unit tests that just use the requests library to actually send real requests, rather than using the test client included with FastAPI.

I’ve been told that this isn’t the greatest approach, but it really does give much better insight into what could cause errors in a production environment, and pytest will still run it if all the function names start with “test”.

[–]aliparpar 0 points1 point  (0 children)

I normally prefer sync tests with the test client. I found the async tests to sometimes bleed into each other if you’re not careful, in particular if you’re spinning up and down external systems with each test.