{"library":"pytest-responses","title":"pytest-responses","description":"pytest-responses is a Pytest plugin that seamlessly integrates the `responses` library into your test suite. It automatically activates `responses`, a utility for mocking the Python `requests` library, across your tests, preventing actual HTTP requests. This helps create fast, reliable, and isolated unit and integration tests. The current version is 0.5.1, and its release cadence is irregular, with updates driven by bug fixes and compatibility needs.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install pytest-responses"],"cli":null},"imports":["import responses\n@responses.activate\ndef test_something(): ..."],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import pytest\nimport requests\nimport responses\n\ndef fetch_user_data(user_id):\n    response = requests.get(f\"https://api.example.com/users/{user_id}\")\n    response.raise_for_status()\n    return response.json()\n\ndef test_fetch_user_data_success(responses):\n    # Add a mock response for the specific URL and method\n    responses.add(\n        responses.GET,\n        'https://api.example.com/users/123',\n        json={'id': 123, 'name': 'Test User'},\n        status=200\n    )\n\n    # Call the function that makes the HTTP request\n    data = fetch_user_data(123)\n\n    # Assert the returned data and that the mock was called\n    assert data == {'id': 123, 'name': 'Test User'}\n    assert len(responses.calls) == 1\n    assert responses.calls[0].request.url == 'https://api.example.com/users/123'\n\ndef test_fetch_user_data_not_found(responses):\n    responses.add(\n        responses.GET,\n        'https://api.example.com/users/404',\n        status=404\n    )\n\n    with pytest.raises(requests.exceptions.HTTPError) as exc_info:\n        fetch_user_data(404)\n    assert exc_info.value.response.status_code == 404\n    assert len(responses.calls) == 1\n","lang":"python","description":"This quickstart demonstrates how to use the `responses` fixture provided by `pytest-responses` to mock HTTP GET requests. It includes tests for both successful responses and error scenarios, showing how to define expected JSON data and status codes, and how to assert that the mocked endpoint was called.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}