skillfed

pytest-subprocess

A plugin to fake subprocess for pytest

pytest-subprocess v1.6.0 514.9K downloads/30d#6,237 on PyPI117
Permissive license MIT Active released

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-subprocess

uv

uv add pytest-subprocess

poetry

poetry add pytest-subprocess

Installing 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

Development Status :: 5 - Production/StableFramework :: PytestIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.15Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: Testing

Tags

mock subprocess calls in testsfake subprocess for pytesttest code that runs external commandspytest subprocess mockingintercept subprocess.Popenregister fake process outputunit test subprocess without real execution
subprocess-mockingtest-isolation

More Testing packages