Registry / testing / pytest-forked

pytest-forked

JSON →
library1.6.0pypypiunverified

pytest-forked is a pytest plugin that runs tests in isolated forked subprocesses, preventing global state pollution and ensuring test independence. As of version 1.6.0, it supports modern Python (3.7+) and pytest versions, offering a crucial tool for test isolation. It generally follows pytest's release cadence for compatibility updates.

pip install pytest-forked
INSTALL
IMPORT
SIG · PYTEST-FORKED
P
pytest-forked
testingpythonv1.6.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

pytest-forked is typically activated via command-line arguments or `pytest.ini` configuration, not by direct Python imports. This example demonstrates how tests benefit from isolation when `--forked` is used, preventing global state changes in one test from affecting another.

# test_isolation.py import pytest # A module-level variable to simulate global state _global_resource_id = 0 def setup_module(): global _global_resource_id _global_resource_id = 100 # This setup runs once per module def test_isolated_resource_access(): # Each forked test process gets its own copy of the global state # if it's set up before the fork, or a fresh state if set up within the test. assert _global_resource_id == 100 # Simulate modifying a global resource global _global_resource_id _global_resource_id += 1 assert _global_resource_id == 101 def test_another_isolated_resource_access(): # This test, when forked, should start with the original _global_resource_id from setup_module, # not affected by modifications in test_isolated_resource_access. assert _global_resource_id == 100 # To run these tests with forked isolation: # 1. Save the code above as test_isolation.py # 2. Run from your terminal: # pytest --forked test_isolation.py
Debug
Known issues
breakingPython 2.7, 3.4, and 3.5 are no longer supported. Ensure your project uses Python 3.7 or newer to use pytest-forked >= 1.4.0.
fix
Upgrade your Python environment to 3.7 or higher.
affects: >=1.4.0
breakingCompatibility with older pytest versions is dropped. For example, pytest-forked v1.3.0 added support for pytest 6, and v1.2.0 fixed support for pytest 5.4.0+. Using older pytest-forked versions with new pytest, or vice-versa, can lead to unexpected behavior.
fix
Ensure your pytest-forked and pytest versions are compatible. Refer to release notes on GitHub for specific version matrices. Generally, use the latest stable versions of both.
affects: <1.3.0 with pytest>=6.0; <1.2.0 with pytest>=5.4.0
gotchaForking can cause issues with shared resources (e.g., database connections, file handles) if not managed carefully. Resources opened before the fork might become invalid or problematic in child processes. Certain libraries or frameworks may not be 'fork-safe'.
fix
Re-initialize shared resources within each test, or use fixtures with `scope='function'` that are executed after the fork. Avoid opening global resources in `conftest.py` setup code if they are not fork-safe.
affects: all
gotchaLimited support for the `pytest.mark.xfail` marker. While improved in newer versions (v1.2.0+), some edge cases or specific interactions with `xfail` might not behave as expected under forked processes.
fix
Upgrade to pytest-forked v1.2.0 or newer for improved `xfail` support. Thoroughly test `xfail` marked tests when using forked processes to confirm expected behavior.
affects: <1.2.0
Upgrade
Version history
1.6.0latest on PyPI · released Feb 12, 2023
Audit
Dependencies
pytestrequiredpytest is the test runner extended by this plugin.
Agent activity
3 hits · last 30 days
node
2
Resources
pytest-forked — pip install pytest-forked · libregistry