Skip to main content

Mocks for subprocess commands

Project description

MockProcess is a mock for the subprocess module to be used in your unit tests. Right now it only supports the check_output method, but eventually popen will be supported as well.

It is used via a context manager. Here’s a basic example

>>> import subprocess
>>> import unittest
>>> from mockprocess import MockCheckOutput
>>> class Test(unittest.TestCase):
...     def test_simple(self):
...         with MockCheckOutput('hello\n'):
...             output = subprocess.check_output(['/bin/echo', 'hello'])
...         self.assertEqual(output, 'hello\n')
...

If you need to test how you handle errors, you can set an exit code as well.

>>> import subprocess
>>> import unittest
>>> from mockprocess import MockCheckoutput
>>> class Test(unittest.TestCase):
...     def test_exception(self):
...         try:
...             with MockCheckOutput('hello\n', exit_code=127):
...                 subprocess.check_output(['/bin/false'])
...         except subprocess.CalledProcessError as exp:
...             pass
...         else:
...             self.fail('exception was not raised')

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

mockprocess-0.1.tar.gz (2.2 kB view details)

Uploaded Source

File details

Details for the file mockprocess-0.1.tar.gz.

File metadata

  • Download URL: mockprocess-0.1.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mockprocess-0.1.tar.gz
Algorithm Hash digest
SHA256 c43d1d19411efac0a829df1003b849df1c2b85946f830854148d6a4c626dbba3
MD5 fc7bbe44f5b60392fcbcf8936aa0896a
BLAKE2b-256 920b200b23cf2cd8e9df336383ccbce6a3904fcb571dc87cda720d1f60697f26

See more details on using hashes here.

Provenance

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