Registry / testing / testfixtures

testfixtures

JSON →
library12.3.0pypypi✓ verified 26d ago

testfixtures is a collection of helpers and mock objects designed to streamline automated testing in Python. It provides utilities for comparing complex objects, mocking methods and classes (including dates, times, and subprocesses), capturing logging and stream output, managing temporary files and directories, and verifying exceptions and warnings. Currently at version 11.0.0, the library is actively maintained with regular updates and improvements.

pip install testfixtures
INSTALL
IMPORT
SIG · TESTFIXTURES
T
testfixtures
testingpythonv12.3.0
Install
1.7s avg
Import
668ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v8.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.423s · 19MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.7s · import 0.378s · 19MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TempDir
from testfixtures import TempDir
Used for managing temporary files and directories.
compare
from testfixtures import compare
General purpose comparison utility for deep data structures.
Replacer
from testfixtures import Replacer
Context manager for temporarily replacing objects, functions, or methods.

This quickstart demonstrates the `TempDir` context manager, a fundamental tool for safely creating and managing temporary files and directories within tests. The directory and its contents are automatically removed upon exiting the `with` block.

import os from testfixtures import TempDir # Create a temporary directory with TempDir() as d: # Write a file to the temporary directory d.write('my_file.txt', b'Hello, testfixtures!') d.makedir('my_subdir') # Verify contents and structure print(f"Files in temporary directory: {sorted(os.listdir(d.path))}") assert 'my_file.txt' in os.listdir(d.path) assert 'my_subdir' in os.listdir(d.path) with open(os.path.join(d.path, 'my_file.txt'), 'rb') as f: content = f.read() print(f"Content of my_file.txt: {content.decode()}") assert content == b'Hello, testfixtures!' # The temporary directory and its contents are automatically cleaned up here
Debug
Known issues
deprecatedThe `TempDirectory` class has been deprecated as of version 11. It returned strings for paths. New code should use `TempDir`, which returns `pathlib.Path` objects for improved consistency and utility.
fix
Replace `from testfixtures import TempDirectory` with `from testfixtures import TempDir` and update any code expecting string paths to work with `Path` objects, using `.path` or `str(path_object)` if string conversion is necessary.
affects: >=11.0.0
gotchaWhen using `testfixtures.compare` or other comparison helpers with objects that implement their own `__eq__` method (e.g., Django model instances), the order of elements within sequences can unexpectedly matter, leading to subtle test failures.
fix
Ensure your expected and actual sequences have elements in the same order if the object's `__eq__` is order-sensitive. Alternatively, consider using `testfixtures.Comparison` or `testfixtures.compare(..., strict=False)` if order is not relevant for a specific comparison, and inspect the specific behavior of the `__eq__` method for the objects being compared.
affects: All versions
gotchaOver-reliance on broadly scoped test fixtures, especially those modifying global state or external resources without meticulous cleanup, can lead to slow, confusing, and brittle tests with non-deterministic results. Each test should ideally be isolated and repeatable.
fix
Design fixtures with the smallest possible scope and ensure they are properly torn down. Prefer using context managers or decorators provided by `testfixtures` (like `TempDir` or `Replacer`) to limit the scope of side effects. For complex setups, consider helper functions or explicit setup within individual tests to maintain clarity and isolation.
affects: All versions
Upgrade
Version history
12.3.0latest on PyPI · released Jul 8, 2026
Audit
Dependencies
pythonrequiredRequired Python version.
PyYAMLoptionalRequired for YAML format support in TempDir.
tomlkitoptionalRequired for TOML format writing support in TempDir (Python < 3.11 uses tomllib built-in for reading).
SybiloptionalRequired for Sybil parser integration with TempDir.
DjangooptionalOptional helpers for Django model comparison.
TwistedoptionalOptional helpers for Twisted logging assertions.
Agent activity
8 hits · last 30 days
node
6
Resources