How to pytest aiohttp when used in FastAPI
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didnāt find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google āHow to X in FastAPIā and didnāt find any information.
- I already read and followed all the tutorial in the docs and didnāt find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options š
Example Code
from fastapi import FastAPI
import aiohttp
app = FastAPI()
@app.get("/")
async def root():
async with aiohttp.ClientSession() as session:
async with session.get("http://httpbin.org/get") as response:
return response.json()
# I'm not sure how to set up a pytest function to mock and test apihttp
Description
Iāve got a FastAPI application that makes HTTP GET requests to another service using the Requests module. Iād like to make these endpoints asynchronous using aiohttp, but I donāt know how to build pytest functions to mock and test the functions with aiohttp in them.
Operating System
Linux
Operating System Details
This is running on a Docker container with Python 3.9.7
FastAPI Version
0.84.0
Python Version
3.9.7
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Async Tests - FastAPI
Then we can create an AsyncClient with the app, and send async requests to it, using await . import pytest from httpx import...
Read more >Developing and Testing an Asynchronous API with FastAPI ...
This tutorial looks at how to develop and test an asynchronous API with FastAPI, Postgres, pytest, and Docker using Test-driven DevelopmentĀ ...
Read more >How to use pytest-aiohttp fixtures with scope session
I am trying to write tests for aiohttp application. I am using pytest-aiohttp plugin. My intention is to initialize and run the applicationĀ ......
Read more >Integrating AIOHTTP Into a FastAPI App - YouTube
In this video I will show you how to make async requests in your FastAPI app using AIOHTTP. You'll see that it's easy...
Read more >Adding Pytest tests to User auth (part 2)
poetry add pytest-asyncio --dev $ poetry add httpx --dev. Next we need to create AsyncClient fixture for further usage in the tests/conftest.py file....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@humrochagf Thanks for the detailed feedback. Iāll run down that path and see if I can get it working. I need to understand FastAPI, PyTest, and mocking better in general. š
Thanks for the help here everyone! š š
Thanks for reporting back and closing the issue @writeson š