question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How to pytest aiohttp when used in FastAPI

See original GitHub issue

First 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:closed
  • Created a year ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
writesoncommented, Sep 27, 2022

@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. šŸ˜ƒ

0reactions
tiangolocommented, Nov 13, 2022

Thanks for the help here everyone! šŸ‘ šŸ™‡

Thanks for reporting back and closing the issue @writeson šŸ‘

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found