{"id":23141,"date":"2022-05-23T14:22:26","date_gmt":"2022-05-23T08:52:26","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=23141"},"modified":"2022-05-23T14:22:28","modified_gmt":"2022-05-23T08:52:28","slug":"python-mock-context-manager","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/","title":{"rendered":"Everything About Python Mock Context Manager"},"content":{"rendered":"\n<p>In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them in our programs for testing.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#What_is_Mocking\" >What is Mocking?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#About_the_Module\" >About the Module<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Importing_The_Module\" >Importing The Module<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Creating_a_Basic_Mock_Object\" >Creating a Basic Mock Object<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#What_is_a_Context_Manager_in_Python\" >What is a Context Manager in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Creating_a_Mock_Asynchronous_Context_Manager_in_Python\" >Creating a Mock Asynchronous Context Manager in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Python_Mock_Context_Manager_enter_and_exit\" >Python Mock Context Manager __enter__ and __exit__<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Python_Mock_Context_Manager_patch\" >Python Mock Context Manager patch()<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#patch_as_a_Context_Manager\" >patch() as a Context Manager<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#patch_as_a_Decorator\" >patch() as a Decorator<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Implementing_Multiple_Context_Managers_using_a_List_of_Context_Managers_in_Python_33\" >Implementing Multiple Context Managers using a List of Context Managers in Python 3.3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#FAQs\" >FAQs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#Trending_Now\" >Trending Now<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-mocking\"><span class=\"ez-toc-section\" id=\"What_is_Mocking\"><\/span>What is Mocking?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Mocking is the usage of a mock object in order to imitate an actual object in a program. Mocking is a vital tool in order to improve program testing. An example of using Mocks is on programs that give requests to APIs. <\/p>\n\n\n\n<p>Changing certain things within the code while testing its behavior in different conditions can cause errors. Thereby leading to the whole program collapsing. It&#8217;s better to simulate unfavorable conditions using a &#8220;Mock Object.&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-about-the-module\"><span class=\"ez-toc-section\" id=\"About_the_Module\"><\/span>About the Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Mock Objects can be created using the <code>unittest.mock<\/code> library. As of Python 3.10, It is part of the <a href=\"https:\/\/docs.python.org\/3\/library\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python standard library<\/a> and comes under the Development Tools category. <\/p>\n\n\n\n<p>The most important class, <code>Mock<\/code>, allows us to imitate objects within our program with Mock Objects. Try out the following commands using our <a href=\"http:\/\/www.pythonpool.com\/python-compiler\/\" target=\"_blank\" rel=\"noreferrer noopener\">Online Interpreter<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-importing-the-module\"><span class=\"ez-toc-section\" id=\"Importing_The_Module\"><\/span>Importing The Module<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>from unittest.mock import Mock<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-basic-mock-object\"><span class=\"ez-toc-section\" id=\"Creating_a_Basic_Mock_Object\"><\/span>Creating a Basic Mock Object<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The primary class of this module is <code>Mock<\/code>. There are unlimited use cases for this class due to its versatility. Let&#8217;s look at how we can initialize a mock object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfrom unittest.mock import Mock\nmyMock = Mock()\nprint(myMock)\n<\/pre><\/div>\n\n\n<p id=\"h-output\"><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"614\" height=\"41\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/image-7.png\" alt=\"Basic Mock Object\" class=\"wp-image-23152\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/image-7.png 614w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/image-7-300x20.png 300w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-a-context-manager-in-python\"><span class=\"ez-toc-section\" id=\"What_is_a_Context_Manager_in_Python\"><\/span>What is a Context Manager in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Resources can be allocated and released when needed using Python context managers. A standard context manager is defined using the <code>with<\/code> statement. If you want to run two related tasks as a pair, with some commands between them, take the following example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwith open('content\/myfile.txt', 'w') as myFile:\n    myFile.write('Welcome to PythonPool!')\n<\/pre><\/div>\n\n\n<p>The above program opens the file <code>myfile.txt<\/code> and proceeds to write in some text. In this case, after editing the file, there was no need to run <code>close()<\/code>. Python context managers automatically perform the close operation and free up memory<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-mock-asynchronous-context-manager-in-python\"><span class=\"ez-toc-section\" id=\"Creating_a_Mock_Asynchronous_Context_Manager_in_Python\"><\/span>Creating a Mock Asynchronous Context Manager in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>With the help of the modules <em>AsyncMock<\/em> and <em>MagicMock,<\/em> we are able to create Asynchronous Context Managers. An asynchronous context manager allows us to execute within <code>__aenter__<\/code> and <code>__aexit__<\/code>.<\/p>\n\n\n\n<p><code>__aenter__<\/code> and <code>__aexit__<\/code> are the provided instances that return the async function.<\/p>\n\n\n\n<p>The syntax for creating an asynchronous context manager is <code>async with<\/code>. Let&#8217;s look at the following Asynchronous context manager class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass AsyncContextManager: \n    async def __aenter__(self): \n        await log('Within the Context Manager') \n \n    async def __aexit__(self, exc_type, exc, tb): \n        await log('Outside the Context Manager')\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-mock-context-manager-enter-and-exit\"><span class=\"ez-toc-section\" id=\"Python_Mock_Context_Manager_enter_and_exit\"><\/span>Python Mock Context Manager <code>__enter__<\/code> and <code>__exit__<\/code><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/www.pythonpool.com\/solved-attributeerror-__enter__\/\" target=\"_blank\" rel=\"noreferrer noopener\">The<code> __enter__<\/code><\/a><\/strong><\/span> and<code> __exit__<\/code> methods of a context manager are the most important. The <code>__enter__<\/code> method is called whenever a context manager is invoked on a class.<\/p>\n\n\n\n<p>The <code>__exit__<\/code> method is executed regardless of the result of the<code> __enter__ <\/code>block. Let&#8217;s refer to the following example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwith myContext(foo) as bar:\n    do_stuff(bar)\n\ncontextObject = myContext(foo)\nbar = contextObject.__enter__()\ntry:\n    do_stuff(bar)\nfinally:\n    contextObject.__exit__(None)\n\n<\/pre><\/div>\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-golf monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-mock-context-manager-patch\"><span class=\"ez-toc-section\" id=\"Python_Mock_Context_Manager_patch\"><\/span>Python Mock Context Manager <code>patch()<\/code><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The standard module <code>unittest.mock()<\/code> provides a tool for mocking program objects called <code>patch()<\/code>. It searches for the object within a program and automatically replaces it with an <code>Mock<\/code> object.<\/p>\n\n\n\n<p>The <code>patch()<\/code> is usually used as a decorator or as a context manager. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-patch-as-a-context-manager\"><span class=\"ez-toc-section\" id=\"patch_as_a_Context_Manager\"><\/span><code>patch()<\/code> as a Context Manager<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>For the following cases, a context manager is used:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Only objects within the test scope should be replaced with a <code>mock<\/code> object.<\/li><li>Usage of multiple decorators affects the test result&#8217;s readability.<\/li><\/ul>\n\n\n\n<p>In order to use <code>patch()<\/code> as a context manager,  the <code>with<\/code> statement is used.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport unittest\nfrom requests.exceptions import Timeout\nfrom unittest.mock import patch\n\nclass TestClass(unittest.TestCase):\n    def testFunction(self):\n        with patch('mySample.requests') as mock_requests:\n            mock_requests.get.side_effect = Timeout\n            with self.assertRaises(Timeout):\n                getRequests()\n                mock_requests.get.assert_called_once()\n\nif __name__ == '__main__':\n    unittest.main()\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-patch-as-a-decorator\"><span class=\"ez-toc-section\" id=\"patch_as_a_Decorator\"><\/span><code>patch()<\/code> as a Decorator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can use <code>patch()<\/code> as a function decorator if you wish to mock an object throughout your entire test function. Let&#8217;s look at the following examples<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport unittest\nfrom requests.exceptions import Timeout\nfrom unittest.mock import patch\n\nclass TestClass(unittest.TestCase):\n    @patch('mySample.requests')\n    def testFunction(self, mock_requests):\n            mock_requests.get.side_effect = Timeout\n            with self.assertRaises(Timeout):\n                getRequests\n                mock_requests.get.assert_called_once()\n\nif __name__ == '__main__':\n    unittest.main()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-implementing-multiple-context-managers-using-a-list-of-context-managers-in-python-3-3\"><span class=\"ez-toc-section\" id=\"Implementing_Multiple_Context_Managers_using_a_List_of_Context_Managers_in_Python_33\"><\/span>Implementing Multiple Context Managers using a List of Context Managers in Python 3.3<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>From Python 3.3, it is possible to enter an n-length list of context managers using the class <code>contextlib.ExitStack<\/code>. <code>contextlib.ExitStack<\/code> is a context manager that allows us to combine multiple other context managers and clean up functions. <\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwith ExitStack() as stack:\n    for eachManager in contextManagers:\n        stack.enter_context(eachManager)\n<\/pre><\/div>\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-alpha monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1652278142742\"><strong class=\"schema-faq-question\">Why do we care about mocking context managers?<\/strong> <p class=\"schema-faq-answer\">Mocking context managers allow us to replicate the properties of default context managers. A context manager allocates resources for tasks like establishing a connection and initiating a clean process once it&#8217;s over. These steps can be replicated using a mock context manager.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1652278327531\"><strong class=\"schema-faq-question\">Why do we need mocks to write tests?<\/strong> <p class=\"schema-faq-answer\">Changing certain things within the code while testing can cause problems. Therefore replacing particular objects with mocks allows us to create test scenarios without affecting the actual program.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1652278552500\"><strong class=\"schema-faq-question\">What are the arguments in unittest.mock.patch?<\/strong> <p class=\"schema-faq-answer\">Here is the complete syntax and arguments of the function.<br\/><br\/><code>unittest.mock.patch(<em>target<\/em>,\u00a0<em>new=DEFAULT<\/em>,\u00a0<em>spec=None<\/em>,\u00a0<em>create=False<\/em>,\u00a0<em>spec_set=None<\/em>,\u00a0<em>autospec=None<\/em>,\u00a0<em>new_callable=None<\/em>,\u00a0<em>**kwargs<\/em>)<\/code><\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We have looked at the concept of Mocking and Context managers. We have also looked at how those two concepts come together and allow easy testing of certain or all objects within our program base. Modules like <code><a href=\"http:\/\/www.pythonpool.com\/python-unittest-vs-pytest\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\"><strong>unittest<\/strong><\/span><\/a><\/code> and <code>AsyncMock<\/code> show their flexibility and versatility when it comes to creating mocks.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-delta monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-2\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Now\"><\/span>Trending Now<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 11, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method.webp 1200w \" alt=\"[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them in our &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Everything About Python Mock Context Manager\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#more-23141\" aria-label=\"More on Everything About Python Mock Context Manager\">Read more<\/a><\/p>\n","protected":false},"author":28,"featured_media":23232,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[5266,5268,5267,5269,5270,5271],"class_list":["post-23141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-mock-context-manager-python","tag-mocking-context-manager-python","tag-python-mock-async-context-manager","tag-python-mock-patch-context-manager","tag-python-mock-with-context-manager","tag-python-unittest-mock-context-manager","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Everything About Python Mock Context Manager - Python Pool<\/title>\n<meta name=\"description\" content=\"In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them for testing.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Everything About Python Mock Context Manager\" \/>\n<meta property=\"og:description\" content=\"In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them in our programs for testing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-23T08:52:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-05-23T08:52:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Vishnu Venkatesh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishnu Venkatesh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/\"},\"author\":{\"name\":\"Vishnu Venkatesh\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/5e8aa48e7557450d345c829305550688\"},\"headline\":\"Everything About Python Mock Context Manager\",\"datePublished\":\"2022-05-23T08:52:26+00:00\",\"dateModified\":\"2022-05-23T08:52:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/\"},\"wordCount\":716,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp\",\"keywords\":[\"mock context manager python\",\"mocking context manager python\",\"python mock async context manager\",\"python mock patch context manager\",\"python mock with context manager\",\"python unittest mock context manager\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/\",\"name\":\"Everything About Python Mock Context Manager - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp\",\"datePublished\":\"2022-05-23T08:52:26+00:00\",\"dateModified\":\"2022-05-23T08:52:28+00:00\",\"description\":\"In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them for testing.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278142742\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278327531\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278552500\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp\",\"width\":1200,\"height\":628,\"caption\":\"python mock context manager\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Everything About Python Mock Context Manager\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/5e8aa48e7557450d345c829305550688\",\"name\":\"Vishnu Venkatesh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d399a4488a19871b08a11ebd884e233b04c76d12edc4c7278a79d69df853d75?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d399a4488a19871b08a11ebd884e233b04c76d12edc4c7278a79d69df853d75?s=96&d=wavatar&r=g\",\"caption\":\"Vishnu Venkatesh\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278142742\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278142742\",\"name\":\"Why do we care about mocking context managers?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Mocking context managers allow us to replicate the properties of default context managers. A context manager allocates resources for tasks like establishing a connection and initiating a clean process once it's over. These steps can be replicated using a mock context manager.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278327531\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278327531\",\"name\":\"Why do we need mocks to write tests?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Changing certain things within the code while testing can cause problems. Therefore replacing particular objects with mocks allows us to create test scenarios without affecting the actual program.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278552500\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278552500\",\"name\":\"What are the arguments in unittest.mock.patch?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Here is the complete syntax and arguments of the function.<br \/><br \/>unittest.mock.patch(<em>target<\/em>,\u00a0<em>new=DEFAULT<\/em>,\u00a0<em>spec=None<\/em>,\u00a0<em>create=False<\/em>,\u00a0<em>spec_set=None<\/em>,\u00a0<em>autospec=None<\/em>,\u00a0<em>new_callable=None<\/em>,\u00a0<em>**kwargs<\/em>)\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Everything About Python Mock Context Manager - Python Pool","description":"In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them for testing.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/","og_locale":"en_US","og_type":"article","og_title":"Everything About Python Mock Context Manager","og_description":"In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them in our programs for testing.","og_url":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/","og_site_name":"Python Pool","article_published_time":"2022-05-23T08:52:26+00:00","article_modified_time":"2022-05-23T08:52:28+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp","type":"image\/webp"}],"author":"Vishnu Venkatesh","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Vishnu Venkatesh","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/"},"author":{"name":"Vishnu Venkatesh","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/5e8aa48e7557450d345c829305550688"},"headline":"Everything About Python Mock Context Manager","datePublished":"2022-05-23T08:52:26+00:00","dateModified":"2022-05-23T08:52:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/"},"wordCount":716,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp","keywords":["mock context manager python","mocking context manager python","python mock async context manager","python mock patch context manager","python mock with context manager","python unittest mock context manager"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-mock-context-manager\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/","url":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/","name":"Everything About Python Mock Context Manager - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp","datePublished":"2022-05-23T08:52:26+00:00","dateModified":"2022-05-23T08:52:28+00:00","description":"In this article, we will be discussing the concept of Python Mocking and Context Managers and how we are able to use them for testing.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278142742"},{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278327531"},{"@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278552500"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-mock-context-manager\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2022\/05\/python-mock-context-manager.webp","width":1200,"height":628,"caption":"python mock context manager"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Everything About Python Mock Context Manager"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/5e8aa48e7557450d345c829305550688","name":"Vishnu Venkatesh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d399a4488a19871b08a11ebd884e233b04c76d12edc4c7278a79d69df853d75?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d399a4488a19871b08a11ebd884e233b04c76d12edc4c7278a79d69df853d75?s=96&d=wavatar&r=g","caption":"Vishnu Venkatesh"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278142742","position":1,"url":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278142742","name":"Why do we care about mocking context managers?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Mocking context managers allow us to replicate the properties of default context managers. A context manager allocates resources for tasks like establishing a connection and initiating a clean process once it's over. These steps can be replicated using a mock context manager.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278327531","position":2,"url":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278327531","name":"Why do we need mocks to write tests?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Changing certain things within the code while testing can cause problems. Therefore replacing particular objects with mocks allows us to create test scenarios without affecting the actual program.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278552500","position":3,"url":"https:\/\/www.pythonpool.com\/python-mock-context-manager\/#faq-question-1652278552500","name":"What are the arguments in unittest.mock.patch?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Here is the complete syntax and arguments of the function.<br \/><br \/>unittest.mock.patch(<em>target<\/em>,\u00a0<em>new=DEFAULT<\/em>,\u00a0<em>spec=None<\/em>,\u00a0<em>create=False<\/em>,\u00a0<em>spec_set=None<\/em>,\u00a0<em>autospec=None<\/em>,\u00a0<em>new_callable=None<\/em>,\u00a0<em>**kwargs<\/em>)","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/23141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=23141"}],"version-history":[{"count":21,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/23141\/revisions"}],"predecessor-version":[{"id":23236,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/23141\/revisions\/23236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/23232"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=23141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=23141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=23141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}