pytest-subprocess
A plugin to fake subprocess for pytest
What it is and what it does
pytest-subprocess is a pytest plugin that intercepts calls to subprocess.Popen and its higher-level wrappers (subprocess.run, subprocess.call, subprocess.check_call, subprocess.check_output) to replace them with fake implementations. Instead of executing real system commands during tests, you register expected commands and their outputs using the fp fixture, which then returns those outputs when your code calls subprocess. This eliminates test dependencies on external tools, makes tests faster and more reliable, and lets you simulate error conditions and edge cases that would be hard to trigger with real processes.
The plugin supports registering different outputs for repeated calls to the same command, passing input to stdin with custom handling logic, allowing unregistered commands to fall through to real subprocess execution, and using callbacks to inject custom behavior. It works across all subprocess functions because they all ultimately call Popen internally. The fixture is available both as fp and the longer form fake_process.
Use it for:
- Test code that shells out to git, docker, or other CLI tools without requiring those tools to be installed or available in CI
- Simulate subprocess failures and non-zero exit codes to verify error handling paths in your application
- Mock multi-step command sequences where each call returns different output, testing state-dependent subprocess behavior
- Verify that your code passes the correct arguments and environment to subprocess without actually executing them
- Test subprocess input/output handling by registering stdin callbacks that transform input into output
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Pytest plugin that intercepts and fakes subprocess calls, allowing you to register mock process outputs and behaviors without executing real commands.
Yes. This is a mature, actively maintained plugin with no security issues, low install friction, and a permissive license. It directly solves the common testing problem of isolating code that calls external processes. Install it if your test suite includes code that uses subprocess.
Install
pytest-subprocess on PyPI
pip
pip install pytest-subprocessuv
uv add pytest-subprocesspoetry
poetry add pytest-subprocessInstalling pytest-subprocess
Before you install
Low friction install with a single runtime dependency (pytest). The package is actively maintained with a recent release and no known vulnerabilities, making it straightforward to add to test suites.
License in practice
MIT license is permissive, allowing use in commercial and open-source projects with minimal restrictions—just include the license text.
Quickstart
pip install pytest-subprocess
import subprocess
def test_example(fp):
fp.register(["echo", "hello"], stdout="hello")
result = subprocess.run(["echo", "hello"], capture_output=True, text=True)
assert result.stdout == "hello"
Verify before relying
- Whether the plugin works with subprocess.Popen subclasses or only the standard library implementation
- Performance overhead when registering many subprocess calls in a single test suite
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.6) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — pytest |
| Maintenance | actively maintained — 96 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 514,874/month — #6,237 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_subprocess-1.6.0-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
pytest-console-scriptsA pytest plugin that runs Python console…
permissive · top 15,000 on PyPI
pytest-forkedA pytest plugin that runs each test in a forked…
permissive · top 5,000 on PyPI
pytest-responsesAutomatically mocks HTTP requests across your…
permissive · top 15,000 on PyPI
EasyProcessEasyProcess wraps Python's subprocess module to…
permissive · top 5,000 on PyPI
scripttestscripttest runs command-line applications in…
permissive · top 15,000 on PyPI
pytest-mockProvides a pytest fixture that wraps the mock…
permissive · top 1,000 on PyPI
pytest-dependsA pytest plugin that lets you declare…
permissive · top 15,000 on PyPI
pytest_httpserverA pytest fixture and standalone context manager…
unclear · top 5,000 on PyPI
pytest-httpx2A pytest plugin that mocks httpx2 HTTP requests…
permissive · top 15,000 on PyPI
pytest-reportportalA pytest plugin that integrates test execution…
permissive · top 5,000 on PyPI