HTTPretty: Intercepting HTTP Requests for Python Testing
This repository profile is provided by osrepos.com, an open source repository discovery platform.

Summary
HTTPretty is a powerful Python library designed to intercept HTTP requests at the socket level, effectively faking the entire socket module. It provides a robust solution for mocking external HTTP services, making it ideal for test-driven development and reliable API integration testing. Developers can use HTTPretty to simulate various HTTP responses, ensuring comprehensive and isolated testing environments.
Repository Information
Topics
Click on any tag to explore related repositories
Use at your own risk
OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.
Introduction
HTTPretty is a Python library created by Gabriel Falcão that provides a comprehensive solution for mocking HTTP client requests. It operates by intercepting HTTP requests at the Python socket level, effectively faking the entire socket module. This capability makes HTTPretty an invaluable tool for developers working on API integrations, enabling them to simulate external service responses without making actual network calls. Inspired by FakeWeb, HTTPretty simplifies the process of creating isolated and repeatable tests for applications that interact with web services.
Installation
To get started with HTTPretty, you can easily install it using pip:
pip install httpretty
Examples
Simple Example
This example demonstrates how to register a GET request and assert the response and request details.
import sure
import httpretty
import requests
@httpretty.activate(verbose=True, allow_net_connect=False)
def test_httpbin():
httpretty.register_uri(
httpretty.GET,
"https://httpbin.org/ip",
body='{"origin": "127.0.0.1"}'
)
response = requests.get('https://httpbin.org/ip')
response.json().should.equal({'origin': '127.0.0.1'})
httpretty.latest_requests().should.have.length_of(1)
httpretty.last_request().should.equal(httpretty.latest_requests()[0])
httpretty.last_request().body.should.equal('{"origin": "127.0.0.1"}')
Checking Multiple Responses
This example shows how to register multiple responses for the same URL and verify the request bodies.
@httpretty.activate(verbose=True, allow_net_connect=False)
def test_post_bodies():
url = 'http://httpbin.org/post'
httpretty.register_uri(httpretty.POST, url, status=200)
httpretty.register_uri(httpretty.POST, url, status=400)
requests.post(url, data={'foo': 'bar'})
requests.post(url, data={'zoo': 'zoo'})
assert 'foo=bar' in httpretty.latest_requests()[0].body
assert 'zoo=bar' in httpretty.latest_requests()[1].body
Why Use HTTPretty?
HTTPretty is essential for robust software development, especially when dealing with external APIs. Its primary benefits include:
- Test-Driven Development (TDD) of API Integrations: Facilitates writing tests for API clients before or during implementation, ensuring correct behavior.
- Faking Responses of External APIs: Allows developers to simulate various API responses, including success, failure, and edge cases, without relying on actual external services.
- Recording and Playback of HTTP Requests: Useful for debugging, performance testing, and creating repeatable test scenarios.
- Isolation and Speed: By intercepting requests at the socket level, HTTPretty ensures tests are isolated from network latency and external service availability, leading to faster and more reliable test suites.
Links
Related repositories
Similar repositories that may be relevant next.

Mixer: A Powerful Fixture Replacement for Python ORMs and ODMs
August 3, 2026
Mixer is a versatile Python library designed to replace fixtures and generate test data efficiently. It supports various ORMs and ODMs, including Django, SQLAlchemy, Flask-SQLAlchemy, Mongoengine, and Marshmallow, making it an invaluable tool for testing and development workflows.

fake2db: Generate Custom Test Databases with Fake Data
August 2, 2026
fake2db is a powerful Python utility designed to create custom test databases populated with fake, yet valid, data. It supports a wide array of popular database systems, including SQLite, MySQL, PostgreSQL, MongoDB, Redis, and CouchDB. This tool is ideal for developers and testers needing quick, realistic data for testing and development environments.

Mimesis: A Powerful Python Library for Realistic Fake Data Generation
August 2, 2026
Mimesis is a robust Python library designed for generating fake yet realistic data across various languages and locales. It simplifies the creation of diverse data types, from personal information to financial details. This makes it an invaluable tool for development, testing, and anonymization tasks.

chardet: A Fast and Accurate Python Character Encoding Detector
August 2, 2026
chardet is a powerful Python library designed for universal character encoding detection. The recently rewritten version 7 offers significant improvements in accuracy and speed, making it a robust solution for identifying text encodings. It provides features like language and MIME type detection, along with a flexible API for various use cases.
Source repository
Open the original repository on GitHub.