Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.7k
Add documentation about asynchronous testing (pytest-asyncio and httpx)#1619
Add documentation about asynchronous testing (pytest-asyncio and httpx)#1619tiangolo merged 15 commits intofastapi:masterfromempicano:add-documentation-about-testing-with-httpx
Conversation
codecovbot commentedJun 24, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov Report
@@ Coverage Diff @@## master #1619 +/- ##========================================= Coverage 100.00% 100.00% ========================================= Files 236 237 +1 Lines 7073 7078 +5 =========================================+ Hits 7073 7078 +5
Continue to review full report at Codecov.
|
| @pytest.mark.asyncio | ||
| async def test_root(): | ||
| async with AsyncClient(app=main.app, base_url='http://test') as ac: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You probably need to addtrust_env=False here as well, otherwise the async client might go through a proxy. Not sure if this is an upstream bug though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
My initial thought was to simply follow theHTTPX example for calling into python web apps. Also, HTTPX setssome constraints on the proxies it accepts from the environment, I don't know if that changes anything.
Otherwise:
| asyncwithAsyncClient(app=main.app,base_url='http://test')asac: | |
| asyncwithAsyncClient(app=main.app,base_url='http://test',trust_env=False)asac: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
encode/httpx#1039 I think it might be a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Hm, interesting. What do you propose, we add thetrust_env=False for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yes, I think that's reasonable. Users who have some default network proxy active (e.g. for privacy reasons or in company network situations) will get some bizarre test failures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Hi there, chiming in to confirm that this odd behavior is indeed due to a bug in HTTPX.
So it might be okay to go with this as-is (i.e. without thetrust_env=False hack), keeping in mind thatencode/httpx#1039 looks to be fairly straight-forward to fix (in case anyone wants to give it a go - happy to review a PR, but no expectations of course), so hopefully a fix will be released soon and users equipped with proxies won't encounter this.
empicano commentedAug 4, 2020
@marco-neumann-by just to point out that the issue with httpx that we hung up on is resolved now |
tiangolo commentedAug 8, 2020
Awesome! Great job@empicano ! Thanks for your contribution! 👏 📝 🤓 🍰 Thanks everyone for the discussion (and@florimondmanca for HTTPX 🚀 💙 ). |
Addressing#1273 andKludex/starlette#652, I added a section about asynchronous testing via pytest-asyncio and httpx to the documentation. I also made a small note (+ link) in the beginner tutorial about it.