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 in order to record the calls.
>>> 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.functions module
>>> # (convenience modules for accessing the _CALL_RESULTS list
>>> # will be added in a later release)
>>>
>>> all_results = subprocess_mock.functions._CALL_RESULTS
>>> 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)
>>> all_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
>>>
>>> all_results.clear()
>>> all_results
[]
>>> run_result = subprocess.run(["touch", str(new_file)])
>>> new_file.exists()
True
>>> all_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
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 Distribution
subprocess-mock-0.3.0.tar.gz
(23.7 kB
view hashes)
Built Distribution
Close
Hashes for subprocess_mock-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dce9beb8330b38b656e8dd9b111060a09f9fa2a5ed1ca37ee320280d2f998ff6 |
|
MD5 | bd5e321a1706ef4569ff81279286c37c |
|
BLAKE2b-256 | a086dfb1607bb66dbdb76aeb03102bf3fcdd51bad72f95f4beb1d8b1455c0a78 |