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:
...   '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.

Files for mockprocess, version 0.2
Filename, size File type Python version Upload date Hashes
Filename, size mockprocess-0.2.tar.gz (2.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page