Skip to content

Add documentation about asynchronous testing (pytest-asyncio and httpx) - #1619

Merged
tiangolo merged 15 commits into
fastapi:masterfrom
empicano:add-documentation-about-testing-with-httpx
Aug 8, 2020
Merged

tiangolo merged 15 commits into
fastapi:masterfrom
empicano:add-documentation-about-testing-with-httpx

Conversation

@empicano

Copy link
Copy Markdown
Contributor

Addressing #1273 and Kludex/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.

@codecov

codecov Bot commented Jun 24, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #1619 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1619   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          236       237    +1     
  Lines         7073      7078    +5     
=========================================
+ Hits          7073      7078    +5     
Impacted Files Coverage Ξ”
tests/test_tutorial/test_async_tests/test_main.py 100.00% <100.00%> (ΓΈ)

Continue to review full report at Codecov.

Legend - Click here to learn more
Ξ” = absolute <relative> (impact), ΓΈ = not affected, ? = missing data
Powered by Codecov. Last update d15556b...14c488b. Read the comment docs.


@pytest.mark.asyncio
async def test_root():
async with AsyncClient(app=main.app, base_url='http://test') as ac:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You probably need to add trust_env=False here as well, otherwise the async client might go through a proxy. Not sure if this is an upstream bug though.

Copy link
Copy Markdown
Contributor Author

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 the HTTPX example for calling into python web apps. Also, HTTPX sets some constraints on the proxies it accepts from the environment, I don't know if that changes anything.

Otherwise:

Suggested change
async with AsyncClient(app=main.app, base_url='http://test') as ac:
async with AsyncClient(app=main.app, base_url='http://test', trust_env=False) as ac:

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Contributor Author

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 the trust_env=False for now?

Copy link
Copy Markdown

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.

Copy link
Copy Markdown

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 the trust_env=False hack), keeping in mind that encode/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

empicano commented Aug 4, 2020

Copy link
Copy Markdown
Contributor Author

@marco-neumann-by just to point out that the issue with httpx that we hung up on is resolved now

@tiangolo
tiangolo merged commit 2fd2843 into fastapi:master Aug 8, 2020
@tiangolo

tiangolo commented Aug 8, 2020

Copy link
Copy Markdown
Member

Awesome! Great job @empicano ! Thanks for your contribution! πŸ‘ πŸ“ πŸ€“ 🍰

Thanks everyone for the discussion (and @florimondmanca for HTTPX πŸš€ πŸ’™ ).

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.

4 participants