{"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":"python","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"},{"fix":"To check if any request was made and matched within the `aioresponses` context, you can inspect `m.calls` (e.g., `assert len(m.calls) > 0`). For specific mocks, use methods like `assert_called()` on the `RequestMatch` object returned when defining the mock (e.g., `mock_obj = m.get(...)`, then `mock_obj.assert_called()`).","message":"The `aioresponses` context manager object (e.g., `m` in `with aioresponses() as m:`) does not expose a `.called` attribute similar to `unittest.mock.Mock` objects. Attempting to access it will raise an `AttributeError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To check if a specific mock was called, use `mock_object.called` where `mock_object` is the return value of `m.get()`, `m.post()`, etc. To check the history of all matched requests, use `m.history` (available from `aioresponses >= 0.6.0`). Alternatively, iterate through `m._matches` and check `RequestCall.called` for each matched request.","message":"The `aioresponses` context manager (or fixture) does not have a top-level `called` attribute to check if any mock was activated. Attempts to access `m.called` will raise an `AttributeError`. Instead, inspect the `history` attribute (available from `aioresponses >= 0.6.0`) or specific mock objects (e.g., `m.get(...).called`) to determine if requests were made and matched.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'activ':47 'aiohttp':25,60,67 'aiohttp.clientsession':18 'aiorespons':1,2 'allow':8 'api':63 'applic':38 'async':68 'asynchron':21 'asyncio':37,69 'cadenc':54 'compat':57 'easili':11 'enabl':31 'extern':42 'fast':34 'focus':55 'http':14 'interact':40 'intercept':24 'isol':32 'librari':6,45 'made':16 'maintain':48 'mock':12,65 'newer':59 'predefin':29 'provid':28 'python':5 'refin':64 'releas':53 'request':15,26 'respons':30 'servic':43 'somewhat':51 'sporad':52 'test':22,35,66 'version':61","created_at":"2026-04-09T03:42:33.859438+00:00","updated_at":"2026-04-15T19:38:22.009756+00:00","problems":[{"fix":"Install the package using pip: 'pip install aioresponses'.","cause":"The 'aioresponses' package is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'aioresponses'"},{"fix":"Use the correct import: 'from aioresponses import aioresponses'.","cause":"Incorrect import statement; 'aioresponses' is a class within the 'aioresponses' module.","error":"ImportError: cannot import name 'aioresponses' from 'aioresponses'"},{"fix":"Use 'aioresponses' as a decorator: '@aioresponses()' or as a context manager: 'with aioresponses() as m:'.","cause":"Attempting to call 'aioresponses' directly without using it as a decorator or context manager.","error":"TypeError: 'aioresponses' object is not callable"},{"fix":"Ensure 'aioresponses' is used as a decorator or within a context manager, and that HTTP methods are mocked correctly within that scope.","cause":"Trying to use HTTP methods like 'get' directly on the 'aioresponses' object without proper setup.","error":"AttributeError: 'aioresponses' object has no attribute 'get'"},{"fix":"Use 'await' directly in async functions or use 'nest_asyncio' to allow nested event loops in interactive environments.","cause":"Calling 'asyncio.run()' or 'loop.run_until_complete()' inside an already running event loop, often in interactive environments like Jupyter notebooks.","error":"RuntimeError: This event loop is already running"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.7.8","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/pnuckowski/aioresponses","docs":null,"changelog":null,"pypi":"https://pypi.org/project/aioresponses/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","http-networking","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":"verified"}}