{"id":1665,"library":"pytest-forked","title":"pytest-forked","description":"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.","status":"active","version":"1.6.0","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-forked","tags":["pytest","testing","plugin","forking","isolation","subprocess"],"install":[{"cmd":"pip install pytest-forked","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"pytest is the test runner extended by this plugin.","package":"pytest","optional":false}],"imports":[],"quickstart":{"code":"# test_isolation.py\nimport pytest\n\n# A module-level variable to simulate global state\n_global_resource_id = 0\n\ndef setup_module():\n    global _global_resource_id\n    _global_resource_id = 100 # This setup runs once per module\n\ndef test_isolated_resource_access():\n    # Each forked test process gets its own copy of the global state\n    # if it's set up before the fork, or a fresh state if set up within the test.\n    assert _global_resource_id == 100\n\n    # Simulate modifying a global resource\n    global _global_resource_id\n    _global_resource_id += 1\n    assert _global_resource_id == 101\n\ndef test_another_isolated_resource_access():\n    # This test, when forked, should start with the original _global_resource_id from setup_module,\n    # not affected by modifications in test_isolated_resource_access.\n    assert _global_resource_id == 100\n\n# To run these tests with forked isolation:\n# 1. Save the code above as test_isolation.py\n# 2. Run from your terminal:\n#    pytest --forked test_isolation.py","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.7 or higher.","message":"Python 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.","severity":"breaking","affected_versions":">=1.4.0"},{"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.","message":"Compatibility 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.","severity":"breaking","affected_versions":"<1.3.0 with pytest>=6.0; <1.2.0 with pytest>=5.4.0"},{"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.","message":"Forking 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'.","severity":"gotcha","affected_versions":"all"},{"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.","message":"Limited 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.","severity":"gotcha","affected_versions":"<1.2.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}