gabrielfalcao

HTTPretty: Mock HTTP Requests in Python Tests

HTTPretty intercepts Python HTTP traffic at the socket level and returns registered fake responses. It helps developers test API integrations without relying on live services, and can capture requests for assertions or playback.

Stars
2.1k
Forks
285
Stars, last 7 days
+0

Our take

Health score

31/100

How it is scored
Activity
20
Community
60
Issues
21
Pull requests
21

+0 stars in the last 7 days

Overview

HTTPretty is a Python library for replacing real HTTP responses with registered fakes during tests. It works at the socket level, so code using an HTTP client can be exercised without contacting the external service.

It is aimed at Python developers testing API integrations and other code that makes HTTP requests. You can also inspect captured requests, making it useful for checking what a client sent as well as how it handles a response. See the GitHub repository and documentation for details.

Key Features

  • Intercepts HTTP requests at the Python socket level.
  • Registers fake responses for specific HTTP methods and URLs.
  • Supports multiple registered responses for repeated requests to the same URL.
  • Captures requests so tests can inspect their bodies and count.
  • Can be used as a test decorator to activate interception around a test.
  • Supports disabling network connections during a test.

Use Cases

  • API developers can test client behavior against predictable responses without depending on a live service.
  • Test-driven development teams can build and run integration tests before an external API is available.
  • Maintainers can verify request methods and payloads sent by code that uses an HTTP client.
  • Developers can test how code handles different responses to successive requests.

Getting Started

Install with pip:

pip install httpretty

See the README and documentation for usage details.

Alternatives

  • httmock: httmock mocks HTTP requests through the requests library, while HTTPretty intercepts traffic at the socket level for broader client compatibility.
  • python-mocket: Mocket intercepts socket and SSL traffic for HTTP and other protocols, while HTTPretty focuses on mocking HTTP responses and recording requests.
ProjectLanguageLicenseStarsStatus
HTTPrettyPythonMIT2.1kNot checked yet
httmockPythonOther472Inactive
python-mocketPythonBSD-3-Clause311Active

Considerations

  • HTTPretty intercepts traffic by faking socket behavior, so tests that activate it should account for its effect on network requests within that scope.
  • The README lists Python 3.6 through 3.9 as supported. Check the project documentation and current environment before relying on compatibility with other versions.

Found this useful?

Share it with someone who would like HTTPretty.

Comparisons