{"id":5754,"date":"2018-01-01T16:03:49","date_gmt":"2018-01-01T16:03:49","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5754"},"modified":"2018-01-01T16:03:49","modified_gmt":"2018-01-01T16:03:49","slug":"mocks-and-python","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/mocks-and-python\/","title":{"rendered":"Mocks and Python"},"content":{"rendered":"<p>In my post <a href=\"http:\/\/putridparrot.com\/blog\/unit-testing-in-python\/\" rel=\"noopener\" target=\"_blank\">Unit testing in Python<\/a> I covered the basics of writing unit tests within Python.<\/p>\n<p>Testing also sometimes involves writing stubs and\/or using mock objects. <\/p>\n<p>As Python is a dynamic language which supports duck typing, we can fairly easily create stub objects, but a mocking framework can make things even simpler. However a good mock framework also gives us the ability to check whether a method was called and assert that it was not called, or was called the correct number of times.<\/p>\n<p><strong>Using MagicMock<\/strong><\/p>\n<p>MagicMock is a class within the <em>unittest.mock<\/em> module used to <em>patch<\/em> methods. Patching being the process of swapping out methods with our mock methods.<\/p>\n<p>Here&#8217;s a Calculator class with an <em>add<\/em> method<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass Calculator:\r\n    def add(self, a, b):\r\n        return a + b\r\n<\/pre>\n<p>Let&#8217;s assume we want to mock\/patch the add method and ensure it&#8217;s called a certain number of times<\/p>\n<p><em>Note: this is a contrived example as we are explicitly calling the add method, thus know how many times it&#8217;s called, but it&#8217;s a simple enough example to see how things fit together.<\/em> <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom unittest.mock import MagicMock \r\n\r\nclass CalculatorTests(TestCase):\r\n\r\n    @classmethod\r\n    def setUpClass(cls):\r\n        cls.c = Calculator()\r\n\r\n    def test_add(self):\r\n        self.c.add = MagicMock(name='add')\r\n        self.c.add.return_value = 5\r\n\r\n        self.assertEqual(5, self.c.add(3, 2))\r\n        self.c.add.assert_called_once_with(3, 2)\r\n<\/pre>\n<p>In the above we&#8217;re using <em>assertEqual<\/em> just to demonstrate the use of the <em>return_value<\/em>. The call to the <em>add<\/em> method within the <em>assertEqual<\/em> demonstrates how we still call our patched method in the same way and then finally we use <em>assert_called_once_with(3, 2)<\/em> to assert that the <em>add<\/em> method was call once and with the expected parameters.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/unittest.mock.html#module-unittest.mock\" rel=\"noopener\" target=\"_blank\">unittest.mock \u2014 mock object library<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my post Unit testing in Python I covered the basics of writing unit tests within Python. Testing also sometimes involves writing stubs and\/or using mock objects. As Python is a dynamic language which supports duck typing, we can fairly easily create stub objects, but a mocking framework can make things even simpler. However a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[195],"tags":[],"class_list":["post-5754","post","type-post","status-publish","format-standard","hentry","category-python"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5754"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5754\/revisions"}],"predecessor-version":[{"id":5757,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5754\/revisions\/5757"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}