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 a subprocess_mock.ModuleFunctions instance to patch subprocess module functions in unit tests in order to record the calls and - if required - inject called process output and returncode (currently, only the patch method for subprocess.run() has been implemented).

>>> 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
>>> # The process not called with normal effects,
>>> # but a call is recorded in the subprocess_mock.ModuleFunctions() object
>>>
>>> smf = subprocess_mock.ModuleFunctions()
>>> with patch("subprocess.run", new=smf.run):
...     run_result = subprocess.run(["touch", str(new_file)])
...
>>> run_result
CompletedProcess(args=['touch', 'testfile.txt'], returncode=0)
>>> smf.call_results
[(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, no call is recorded
>>>
>>> smf.reset()
>>> smf.call_results
[]
>>> run_result = subprocess.run(["touch", str(new_file)])
>>> new_file.exists()
True
>>> smf.call_results
[]
>>>

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.2.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

subprocess_mock-0.2.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: subprocess-mock-0.2.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for subprocess-mock-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3da47c08534834daf9e9b3d730c57880f6c659cc69d99bc288573c10f9729b44
MD5 c45b99a1949cb8d58ddcae0b25af0a23
BLAKE2b-256 e4f907d049fc53323be879ae4f239c11e010e3370c13394a7a55ca5898731e66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for subprocess_mock-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f202d0f1127aec21e4810702f4e8af4e845e7fee9f9425ff352585c62c522d7
MD5 de7d9da9e4922737c27958cb4978f2b9
BLAKE2b-256 9a096a66e0957f303320e314ff7a9dc0ec8b0bcd48ea1787cc8decddba3e5c11

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