Skip to main content

Subprocess Mock

Project description

Subprocess Mock

Mock objects for the standard library’s subprocess module

pip install subprocess-mock

Installation in a virtual environment is recommended.

Example Usage

Use the functions defined in the subprocess_mock.functions module (and also exposed in the subprocess_mock namespace) to patch subprocess module functions in unit tests.

>>> import pathlib
>>> import subprocess
>>> import subprocess_mock
>>> from unittest.mock import patch
>>>
>>> new_file = pathlib.Path("testfile.txt")
>>> new_file.exists()
False
>>> # Test: call a process with a mock.patched subprocess.run
>>> # No Process is called,
>>> # but a subprocess.CompletedProcess instance is returned.
>>>
>>> with patch("subprocess.run", new=subprocess_mock.run):
...     run_result = subprocess.run(["touch", str(new_file)])
...
>>> run_result
CompletedProcess(args=['touch', 'testfile.txt'], returncode=0)
>>> new_file.exists()
False
>>>
>>> # Counter-test: call the process without patching subprocess.run
>>> # The process is called with normal effects.
>>>
>>> run_result = subprocess.run(["touch", str(new_file)])
>>> new_file.exists()
True
>>>

Further reading

Please see the documentation at https://blackstream-x.gitlab.io/subprocess-mock for detailed usage information.

If you found a bug or have a feature suggestion, please open an issue here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

subprocess-mock-0.4.1.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

subprocess_mock-0.4.1-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file subprocess-mock-0.4.1.tar.gz.

File metadata

  • Download URL: subprocess-mock-0.4.1.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for subprocess-mock-0.4.1.tar.gz
Algorithm Hash digest
SHA256 6635f3046dbe799afc01e2946d926cc037bbabccd120f918ef5f9f1dd648509f
MD5 b0446a6df4bc45762be128a8dd49ea4b
BLAKE2b-256 12dd47e33e1243279446c2b535106e16863a14c34bae25d1f791096c4f190c59

See more details on using hashes here.

File details

Details for the file subprocess_mock-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for subprocess_mock-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 08c1ab058a5f152982fb2b476c160da16dac3e038ea18a52e7c60f126b62e9f5
MD5 4866276f37854d727772aff47d21284a
BLAKE2b-256 058c25cdbea6cbede9382757756dac69bc33e8a6c2203eb57cd5296ca146595d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page