Registry / testing / fixtures

fixtures

JSON →
library4.3.2pypypi✓ verified 23d ago

The `fixtures` library (not to be confused with `pytest` fixtures) provides a Python contract for reusable state and support logic, primarily for unit testing. It includes helper and adaptation logic to simplify writing fixtures and offers glue code for `unittest`-compatible test cases. As of version 4.3.2, released in March 2026, the library is actively maintained and supports Python 3.10 and newer. It offers a set of pre-canned fixtures like `LogHandler`, `MockPatchObject`, and `MonkeyPatch` for common testing scenarios.

pip install fixtures
INSTALL
IMPORT
SIG · FIXTURES
F
fixtures
testingpythonv4.3.2
Install
1.5s avg
Import
369ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.3.2 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.400s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.338s · 19MB
16MB installed
● package 16MB
Code
Verified usage

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

Fixture
from fixtures import Fixture
LogHandler
from fixtures import LogHandler
MockPatchObject
from fixtures import MockPatchObject
MonkeyPatch
from fixtures import MonkeyPatch

This example demonstrates how to create a custom `Fixture` that manages a temporary file. The `with` statement handles the `setUp` and `cleanUp` methods automatically, ensuring resources are properly managed before and after the test operation.

import os from fixtures import Fixture class TemporaryFileFixture(Fixture): def setUp(self): super().setUp() self.temp_file_path = "temp_data.txt" with open(self.temp_file_path, "w") as f: f.write("Some temporary test data.") print(f"Fixture setup: Created {self.temp_file_path}") def cleanUp(self): if os.path.exists(self.temp_file_path): os.remove(self.temp_file_path) print(f"Fixture cleanup: Removed {self.temp_file_path}") super().cleanUp() # Recommended usage as a context manager: def run_test_with_temp_file(): with TemporaryFileFixture() as temp_fixture: print(f"Test running: Accessing {temp_fixture.temp_file_path}") with open(temp_fixture.temp_file_path, "r") as f: content = f.read() assert content == "Some temporary test data." print("Test passed.") if __name__ == "__main__": run_test_with_temp_file()
Debug
Known issues
gotchaIf not utilizing the `with` statement (context manager protocol) or the provided `unittest` integration, you must manually call `fixture.setUp()` before use and `fixture.cleanUp()` afterward to ensure proper resource management and state isolation. Failing to call `cleanUp()` can lead to resource leaks.
fix
Use the fixture as a context manager (`with MyFixture(): ...`) or ensure `setUp()` and `cleanUp()` are explicitly called in your test's setup and teardown methods (e.g., `unittest.TestCase.setUp` and `tearDown`).
affects: All versions
gotchaThe `MonkeyPatch` fixture, while powerful for modifying attributes, has noted complexities when used for patching methods. Refer to the official API documentation for detailed behavioral nuances to avoid unexpected side effects.
fix
Consult the specific documentation for `fixtures.MonkeyPatch` for best practices when patching methods. Consider alternative mocking strategies if encountering issues.
affects: All versions
gotchaWhen reusing fixture instances across multiple test operations, especially with methods like `reset()`, be vigilant about potential state leakage if the `setUp()` and `cleanUp()` logic doesn't fully reset all mutable attributes. This can lead to non-deterministic test results.
fix
Ensure that your `setUp()` and `cleanUp()` methods thoroughly isolate or reset any mutable state your fixture introduces or modifies. Prefer creating fresh fixture instances or using context managers where possible for critical isolation.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'fixtures'
The `fixtures` library, or its common alias `testfixtures`, is not installed in the Python environment.
fix
Install the library using pip: `pip install fixtures`
ModuleNotFoundError: No module named 'testfixtures'
Code is attempting to import the `fixtures` library using its older, but still common, alias `testfixtures`, which is not installed.
fix
Install the library using pip: `pip install testfixtures` (or `pip install fixtures` and update the import statement to `import fixtures` if preferred)
TypeError: __init__() got multiple values for keyword argument 'names'
This error often occurs when trying to initialize `fixtures.LogCapture` (or `fixtures.log_capture` used as a decorator) and passing both positional arguments and a keyword argument `names` or similar conflicting arguments.
fix
Ensure that `LogCapture` or `log_capture` is initialized with either positional arguments or keyword arguments, but not both for the same parameter. For example, use `LogCapture(level=logging.ERROR, names='my_logger')` or `LogCapture('my_logger', level=logging.ERROR)` but not `LogCapture('my_logger', names='my_logger')`.
AttributeError: 'module' object has no attribute 'MonkeyPatch'
This error occurs when trying to access `MonkeyPatch` directly from the top-level `fixtures` module when it should be imported from `fixtures.monkeypatch`.
fix
Change the import statement from `from fixtures import MonkeyPatch` to `from fixtures.monkeypatch import MonkeyPatch` or `import fixtures.monkeypatch` and then use `fixtures.monkeypatch.MonkeyPatch`.
ValueError: I/O operation on closed file.
When using `fixtures.LogHandler` (or `LogCapture`) in conjunction with a test runner like `pytest` that captures stdout/stderr, the underlying stream might be closed by the test runner before `LogHandler` attempts to write to it during teardown or later logging.
fix
Configure the test runner's logging capture (e.g., `pytest -p no:logging` or `log_file_level` in `pytest.ini`) or ensure that custom log handlers manage their streams carefully within the test's lifecycle, potentially using a context manager or ensuring handlers are removed before stream closure.
Upgrade
Version history
4.3.2latest on PyPI · released Mar 25, 2026
Audit
Dependencies
testtoolsoptionalRequired for running the library's own test suite and included with the 'streams' extra (pip install fixtures[streams]).
Agent activity
9 hits · last 30 days
node
8
Resources
fixtures — pip install fixtures · libregistry