Wrapture makes it easy to wrap any function or method so you can trace its calls or override its return value, acting as an alternative to unittest.mock.
Wrapture ( full documentation here ) makes it easy to wrap any function or method such that all access can be traced, or can be overridden to return a different value. — Graham Dumpleton
Wrapture is a small Python library that lets you “wrap” an existing function or method. In practice, wrapping means inserting a thin layer around the original code so that every time the function is called you can see what happened—what arguments were passed, what the function returned, or even replace the return value entirely. Think of it as a reversible “sticky note” you can place on any piece of code without having to edit the code itself.
The library works by creating a proxy object that forwards calls to the original function. Because the proxy sits in front of the real implementation, it can log each call (a technique called tracing) or intervene and supply a different result (a technique called stubbing). The original source file stays untouched, which means you can experiment with behavior, collect diagnostics, or isolate problematic parts of a program without risking permanent changes.
Wrapture is aimed at people who need to stub, spy on, or instrument existing Python code without rewriting it. Typical scenarios include:
These use cases are especially relevant for developers who work with legacy codebases or third‑party packages. For non‑developers, the tool does not provide a direct benefit because it requires writing Python code to define the wrappers.
Wrapture positions itself as “both an alternative to unittest.mock and a way to implement tracing against an existing project.” unittest.mock is a standard library feature that replaces objects with mock objects during tests. Wrapture offers a similar ability to replace return values, but it also emphasizes tracing—the systematic recording of every call—without needing to rewrite the target code. In short, it blends the mocking capability with a built‑in logging mechanism.
The library is shipping now, meaning you can install it from PyPI and start using it immediately. There is no public information about pricing, and the documentation does not list a commercial license, so it appears to be freely available.
Wrapture is not a full‑featured testing framework. While it can replace return values and log calls, it does not provide the broader suite of assertions, test discovery, and fixture management that unittest or pytest offer. Users will still need a conventional test runner for complete test suites.
If you are a Python developer who frequently needs to observe or control the behavior of code you cannot edit—whether for testing, monitoring, or debugging—Wrapture gives you a lightweight, code‑free way to insert tracing or stubbing logic. It is already released and ready to use, but it should be viewed as a complement to, rather than a replacement for, existing testing tools.