{"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.","language":"python","status":"active","last_verified":"Thu Apr 09","install":{"commands":["pip install pytest-forked"],"cli":null},"imports":[],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]},"compatibility":null}