{"id":1313,"library":"aioresponses","title":"aioresponses","description":"aioresponses is a Python library that allows you to easily mock out HTTP requests made by `aiohttp.ClientSession` in your asynchronous tests. It intercepts `aiohttp` requests and provides predefined responses, enabling isolated and fast testing of `asyncio` applications that interact with external services. The library is actively maintained, with a somewhat sporadic release cadence, focusing on compatibility with newer `aiohttp` versions and API refinements.","status":"active","version":"0.7.8","language":"en","source_language":"en","source_url":"https://github.com/pnuckowski/aioresponses","tags":["mocking","testing","aiohttp","async","asyncio"],"install":[{"cmd":"pip install aioresponses","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"aioresponses mocks requests made by aiohttp.ClientSession, so aiohttp is a fundamental dependency for its usage context and is explicitly required by the library.","package":"aiohttp","optional":false}],"imports":[{"symbol":"aioresponses","correct":"from aioresponses import aioresponses"}],"quickstart":{"code":"import asyncio\nfrom aiohttp import ClientSession\nfrom aioresponses import aioresponses\n\nasync def fetch_data(url):\n    async with ClientSession() as session:\n        async with session.get(url) as response:\n            response.raise_for_status() # Raise an exception for bad status codes\n            return await response.json()\n\nasync def main():\n    test_url = \"http://example.com/api/data\"\n    # Mocking the GET request to test_url\n    with aioresponses() as m:\n        m.get(test_url, status=200, payload={\"key\": \"value\"})\n        data = await fetch_data(test_url)\n        print(f\"Fetched data: {data}\")\n        # Assert that the mock was called\n        assert m.called\n        assert test_url in m.calls[0].url\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n","lang":"python","description":"This example demonstrates how to use `aioresponses` as a context manager to mock an HTTP GET request made by `aiohttp.ClientSession`. It sets up a mock for `http://example.com/api/data` to return a 200 status and a JSON payload, then calls an `async` function that uses `aiohttp` to fetch data, and finally asserts that the mock was engaged."},"warnings":[{"fix":"Ensure your project uses `aiohttp >= 3.0.0` to be compatible with `aioresponses >= 0.4.0`. The library currently explicitly requires `aiohttp>=3.0.0`.","message":"Version 0.4.0 dropped support for `aiohttp 1.x` and introduced compatibility with `aiohttp 3.x`. Using `aioresponses >= 0.4.0` with older `aiohttp` versions will lead to errors.","severity":"breaking","affected_versions":">=0.4.0"},{"fix":"Update any code that directly accesses or expects the old class or attribute names to use the new `RequestMatch`, `RequestCall`, and `_matches` names respectively.","message":"Version 0.5.0 introduced significant internal API renames: `MockedResponse` became `RequestMatch`, `method_call` became `RequestCall`, and the internal `_responses` attribute was renamed to `_matches`. This may affect advanced usage or direct inspection of the mock object.","severity":"breaking","affected_versions":">=0.5.0"},{"fix":"Upgrade to `aioresponses >= 0.5.0` to enable repeated executions of mocked requests. If upgrading is not possible, ensure your tests only make a single request per mock setup or explicitly add multiple mocks for the same URL.","message":"Prior to version 0.5.0, a mocked request would only be matched once. Subsequent requests to the same URL or pattern would not use the mock and would either hit the real network or fail.","severity":"gotcha","affected_versions":"<0.5.0"},{"fix":"Structure your code to ensure the `with aioresponses() as m:` block (or the `aioresponses` fixture in `pytest`) is active for the duration of the `aiohttp.ClientSession` that performs the requests. For example, pass the `ClientSession` into the mocked function, or create the `ClientSession` within the mock's scope.","message":"The `aioresponses` context manager (or fixture) must encompass the entire lifecycle of the `aiohttp.ClientSession` and all requests you intend to mock. If the context manager exits before a `ClientSession` makes its request, the mock will no longer be active, leading to real network calls or connection errors.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}