Skip to main content

simple patching for python functions

Project description

simple_mock


Introduction


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


Installation


Install via pip:

$ pip install simple_mock

Install from source:

$ git clone https://github.com/satyavls/simple_mock.git
$ cd simple_mock
$ python setup.py install

Usage

# 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):
        self.simple_mock.clear_all_patch()

    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__':
    unittest.main()

Refer here for more detailed examples

Contributing

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.

License

MIT

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

simple_mock-0.0.1.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file simple_mock-0.0.1.tar.gz.

File metadata

  • Download URL: simple_mock-0.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.12

File hashes

Hashes for simple_mock-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7dd00870d3698e1852e9675023d8d276ea81006ad42b3abe86767751d46634d2
MD5 09be2d6663026ee07a328f62718fc308
BLAKE2b-256 f09bed2931161a35e0bbf7a1fdd6642067b30b9eae23aa7eb5d3148494484777

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page