Skip to main content

simple patching for python functions

Project description



The motivation behind this library is to provide easy way of mocking to python functions while doing the unittest.
This library is based on "mock" module in python


Install via pip:

$ pip install simple_mock

Install from source:

$ git clone
$ cd simple_mock
$ python install


# example - mocking single function to return some value or exception

import unittest
from simple_mock import SimpleMock

# import of function that needs to be patch in test case file
from add_module import add_num

class TestAddNum(unittest.TestCase):

    def setUp(self):
        # create simple mock object 
        self.simple_mock = SimpleMock()

    def tearDown(self):

    def test_add_num(self):
        This is example where add_num function is patched to return the 45 value
        # patch the add_num function to return 45 value
        patched_add_func = self.simple_mock.patch_func(func='add_num', exptd_ret_val=45)
        self.assertEqual(add_num(2, 2), 45)

    def test_add_num_exception(self):
        This is exmaple where add_num function is patched to return exception
        # Patch the add_num function to return ValueError 
        patched_add_func = self.simple_mock.patch_func(func='add_num', exptd_err=ValueError,
                                                       err_msg="exception raise in add_num")
        self.assertRaises(ValueError, add_num)

if __name__ == '__main__':

Refer here for more detailed examples


Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests or examples are appropriate.



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 simple-mock, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size simple_mock-0.0.1.tar.gz (2.9 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