Run marked pytest tests in grouped subprocesses (cross-platform).
Project description
pytest-isolated
A pytest plugin that runs marked tests in isolated subprocesses with intelligent grouping.
Features
- Run tests in fresh Python subprocesses to prevent state pollution
- Group related tests to run together in the same subprocess
- Handles crashes, timeouts, and setup/teardown failures
- Captures stdout/stderr for failed tests
- Works with pytest reporters (JUnit XML, etc.)
- Configurable timeouts to prevent hanging subprocesses
- Cross-platform: Linux, macOS, Windows
Installation
pip install pytest-isolated
Quick Start
Mark tests to run in isolated subprocesses:
import pytest
@pytest.mark.isolated
def test_isolated():
# Runs in a fresh subprocess
assert True
Tests with the same group run together in one subprocess:
@pytest.mark.isolated(group="mygroup")
def test_one():
shared_state.append(1)
@pytest.mark.isolated(group="mygroup")
def test_two():
# Sees state from test_one
assert len(shared_state) == 2
Set timeout per test group:
@pytest.mark.isolated(timeout=30)
def test_with_timeout():
# This group gets 30 second timeout (overrides global setting)
expensive_operation()
Tests without an explicit group are automatically grouped by module.
Configuration
Command Line
# Set isolated test timeout (seconds)
pytest --isolated-timeout=60
# Disable subprocess isolation for debugging
pytest --no-isolation
# Combine with pytest debugger
pytest --no-isolation --pdb
pytest.ini / pyproject.toml
[pytest]
isolated_timeout = 300
isolated_capture_passed = false
Or in pyproject.toml:
[tool.pytest.ini_options]
isolated_timeout = "300"
isolated_capture_passed = false
Use Cases
Testing Global State
@pytest.mark.isolated
def test_modifies_environ():
import os
os.environ["MY_VAR"] = "value"
# Won't affect other tests
@pytest.mark.isolated
def test_clean_environ():
import os
assert "MY_VAR" not in os.environ
Testing Singletons
@pytest.mark.isolated(group="singleton_tests")
def test_singleton_init():
from myapp import DatabaseConnection
db = DatabaseConnection.get_instance()
assert db is not None
@pytest.mark.isolated(group="singleton_tests")
def test_singleton_reuse():
db = DatabaseConnection.get_instance()
# Same instance as previous test in group
Testing Process Resources
@pytest.mark.isolated
def test_signal_handlers():
import signal
signal.signal(signal.SIGTERM, custom_handler)
# Won't interfere with pytest
Output and Reporting
Failed tests automatically capture and display stdout/stderr:
@pytest.mark.isolated
def test_failing():
print("Debug info")
assert False
Works with standard pytest reporters:
pytest --junitxml=report.xml --durations=10
Limitations
Fixtures: Module/session fixtures run in each subprocess group. Cannot share fixture objects between parent and subprocess.
Debugging: Use --no-isolation to run all tests in the main process for easier debugging with pdb or IDE debuggers.
Performance: Subprocess creation adds ~100-500ms per group. Group related tests to minimize overhead.
Advanced
Timeout Handling
pytest --isolated-timeout=30
Timeout errors are clearly reported with the group name and timeout duration.
Crash Detection
If a subprocess crashes, tests in that group are marked as failed with exit code information.
Subprocess Detection
import os
if os.environ.get("PYTEST_RUNNING_IN_SUBPROCESS") == "1":
# Running in subprocess
pass
Troubleshooting
Tests timing out: Increase timeout with --isolated-timeout=600
Missing output: Enable capture for passed tests with isolated_capture_passed = true
Subprocess crashes: Check for segfaults, OOM, or signal issues. Run with -v for details.
Contributing
- Install pre-commit:
pip install pre-commit && pre-commit install - Run tests:
pytest tests/ -v - Open an issue before submitting PRs for new features
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_isolated-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pytest_isolated-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bace41504e55fbe823506e928255fd1d93fe39c3e37b83b71ea98acaed905d78
|
|
| MD5 |
b32a140d1975c0987f6702033f9606de
|
|
| BLAKE2b-256 |
f5f486916e1166078bd7ee1dcf9d5584001bf0ef76ab74d8e0cdb8907a5201fd
|