{"id":59152,"date":"2023-11-17T12:07:24","date_gmt":"2023-11-17T06:37:24","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59152"},"modified":"2023-12-05T16:50:29","modified_gmt":"2023-12-05T11:20:29","slug":"mocking-vs-monkey-patching-in-python","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/mocking-vs-monkey-patching-in-python\/","title":{"rendered":"Mocking VS Monkey Patching in Python"},"content":{"rendered":"<h1>What is Monkey Patching?<\/h1>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59416 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/Czym_jest_monkey_patching_w_Pythonie-1024x536.png\" alt=\"\" width=\"625\" height=\"327\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/11\/Czym_jest_monkey_patching_w_Pythonie-1024x536.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/Czym_jest_monkey_patching_w_Pythonie-300x157.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/Czym_jest_monkey_patching_w_Pythonie-768x402.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/Czym_jest_monkey_patching_w_Pythonie-624x327.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/11\/Czym_jest_monkey_patching_w_Pythonie.png 1200w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><span class=\"l xc xd xe bn xf xg xh xi xj cn\">M<\/span>onkey patching is a concept python where in we can change the behavior or work of code, module, or class at run time without changing the whole code base. For example in big projects, we have some third-party modules which may or may not be working while you are developing. In order to test our piece of code, we can tweak the behavior of that module.<\/p>\n<pre>#test_monkey.py\r\nclass A:\r\ndef func(self):\r\nprint('I am method in class A')\r\n\r\nimport test_monkey\r\ndef monkey_func(self):\r\nprint('Hi I am monkey method')\r\n\r\ntest_monkey.A.func = monkey\r\nobj = test_monkey.A()\r\nobj.func()\r\n\r\n<\/pre>\n<p>Output<\/p>\n<p>Hi I am monkey method<\/p>\n<h2>What is Mocking?<\/h2>\n<p><span class=\"l xc xd xe bn xf xg xh xi xj cn\">M<\/span>ocking is defined as the set of different types of techniques that help achieve the goal of isolating units or pieces of code to test them independently while simulating the environment in which the unit of code exists.<\/p>\n<p>In simple terms, mocking helps developers test a unit of code that may have dependencies on others. For example, I am implementing a caching layer over my database layer, which includes writing code to get\/set data from\/to Redis before performing any database operations to test this functionality. I don\u2019t actually need to set\/get data to\/from Redis I can create a mock function that will create the illusion that I am able to get\/set data to Redis.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59151 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/10\/mocking.jpg\" alt=\"\" width=\"703\" height=\"302\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/10\/mocking.jpg 703w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/mocking-300x129.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/mocking-624x268.jpg 624w\" sizes=\"(max-width: 703px) 100vw, 703px\" \/><\/p>\n<p><i>Want to learn more about mocking and its techniques, check this <a class=\"af yf\" href=\"https:\/\/www.ponicode.com\/shift-left\/what-is-mocking\" target=\"_blank\" rel=\"noopener ugc nofollow\">link<\/a><\/i>.<\/p>\n<h2>How is mocking different from monkey patching?<\/h2>\n<p>Monkey patching replaces a function\/method\/class with another at runtime for testing purposes, fixing a bug, or otherwise changing behavior. On the other hand, mocking uses monkey patching to replace part of your software under test with mock objects. It provides more functionality to write unit tests, such as:<\/p>\n<ul>\n<li>It records how mock objects are being called, so you can test the calling behavior of your code with asserts.<\/li>\n<li>A handy decorator <em class=\"yb\" style=\"font-size: 1rem;\">patch()<\/em><span style=\"font-size: 1rem;\">\u00a0for the actual monkey patching.<\/span><\/li>\n<li>You can make mock objects to return specific values\u00a0<em class=\"yb\">(return_value)<\/em>, and raise specific exceptions (<em class=\"yb\">side_effect<\/em>).<\/li>\n<\/ul>\n<h1 id=\"97e2\" class=\"vn vo rt be vp il vq im in io vr ip iq ir vs is it iu vt iv iw ix vu iy iz vv bj\" data-selectable-paragraph=\"\">Conclusion<\/h1>\n<p>Hope this blog helps you understand the basics of Monkey Patching and Mocking and also helps you in understanding the difference or use case of mocking and monkey patching. For more details on <a class=\"af yf\" href=\"https:\/\/www.javatpoint.com\/monkey-patching-in-python\" target=\"_blank\" rel=\"noopener ugc nofollow\">Monkey Patching<\/a>\u00a0and\u00a0<a class=\"af yf\" href=\"https:\/\/docs.python.org\/3\/library\/unittest.mock.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">Mocking<\/a>\u00a0please refer link.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>What is Monkey Patching? Monkey patching is a concept python where in we can change the behavior or work of code, module, or class at run time without changing the whole code base. For example in big projects, we have some third-party modules which may or may not be working while you are developing. In [&hellip;]<\/p>\n","protected":false},"author":1677,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":539},"categories":[1994],"tags":[1358],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59152"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1677"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59152"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59152\/revisions"}],"predecessor-version":[{"id":59470,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59152\/revisions\/59470"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}