{"id":3655,"date":"2022-07-04T09:20:57","date_gmt":"2022-07-04T09:20:57","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3655"},"modified":"2022-07-09T07:41:41","modified_gmt":"2022-07-09T07:41:41","slug":"python-unittest-mock","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest-mock\/","title":{"rendered":"Python Unittest Mock"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <a href=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest\/\">unittest<\/a> Mock class and how to use it to mock other classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-unittest-mock-class'>Introduction to Python unittest Mock class <a href=\"#introduction-to-python-unittest-mock-class\" class=\"anchor\" id=\"introduction-to-python-unittest-mock-class\" title=\"Anchor for Introduction to Python unittest Mock class\">#<\/a><\/h2>\n\n\n\n<p>Mocks simulate the behaviors of real objects. To test an object that depends on other objects in an isolated manner, you use mock objects to mock the real objects.<\/p>\n\n\n\n<p>To mock objects, you use the <code>unittest.mock<\/code> module. The <code>unittest.mock<\/code> module provides the <code>Mock<\/code> class that allows you to mock other objects.<\/p>\n\n\n\n<p>It also provides the <code>MagicMock<\/code> class that is a subclass of the <code>Mock<\/code> class. Besides the methods and properties of the <code>Mock<\/code> class, the <code>MagicMock<\/code> class has the implementations of all the dunder methods e.g., <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__str__\/\">__str__<\/a> and <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__repr__\/\">__repr__<\/a>.<\/p>\n\n\n\n<p>See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> unittest.mock <span class=\"hljs-keyword\">import<\/span> Mock\n\n\n<span class=\"hljs-comment\"># create a new mock object<\/span>\nmock = Mock()\n\n<span class=\"hljs-comment\"># mock the api function<\/span>\nmock.api.return_value = {\n    <span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>,\n    <span class=\"hljs-string\">'message'<\/span>: <span class=\"hljs-string\">'hello'<\/span>\n}\n<span class=\"hljs-comment\"># call the api function<\/span>\nprint(mock.api())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">{<span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'message'<\/span>: <span class=\"hljs-string\">'hello'<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import the <code>Mock<\/code> class from the <code>unittest.mock<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> unittest.mock <span class=\"hljs-keyword\">import<\/span> Mock<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a new instance of the <code>Mock<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mock = Mock()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, mock the <code>api()<\/code> function and assign its return value to a dictionary:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mock.api.return_value = {\n    <span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>,\n    <span class=\"hljs-string\">'message'<\/span>: <span class=\"hljs-string\">'hello'<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, call the <code>api()<\/code> from the mock object. It&#8217;ll return the assigned value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(mock.api())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we have two mock objects: <code>mock<\/code> &amp; <code>mock.api<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s add the <code>print()<\/code> statement to the program to see how it works:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> unittest.mock <span class=\"hljs-keyword\">import<\/span> Mock\n\n\n<span class=\"hljs-comment\"># create a new mock object<\/span>\nmock = Mock()\nprint(mock)\n\n<span class=\"hljs-comment\"># mock the api function<\/span>\nmock.api.return_value = {\n    <span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>,\n    <span class=\"hljs-string\">'message'<\/span>: <span class=\"hljs-string\">'hello'<\/span>\n}\nprint(mock.api)\n\n<span class=\"hljs-comment\"># call the api<\/span>\nprint(mock.api())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&lt;Mock id=<span class=\"hljs-string\">'1830094470496'<\/span>&gt;\n&lt;Mock name=<span class=\"hljs-string\">'mock.api'<\/span> id=<span class=\"hljs-string\">'1830100086416'<\/span>&gt;\n{<span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'message'<\/span>: <span class=\"hljs-string\">'hello'<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output shows two <code>Mock<\/code> objects.<\/p>\n\n\n\n<p>In short, if you assign a property that doesn&#8217;t exist on the <code>Mock<\/code> object, Python will return a new mock object. Because of this dynamic, you can use the <code>Mock<\/code> class to mock any objects that you want.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='when-to-use-mock'>When to use mock <a href=\"#when-to-use-mock\" class=\"anchor\" id=\"when-to-use-mock\" title=\"Anchor for When to use mock\">#<\/a><\/h2>\n\n\n\n<p>These are cases that you may consider using mocks:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>System calls<\/li><li>Networking<\/li><li>I\/O operation<\/li><li>Clocks &amp; time, timezones<\/li><li>Or other cases whose results are unpredictable<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='why-using-mocks'>Why using mocks <a href=\"#why-using-mocks\" class=\"anchor\" id=\"why-using-mocks\" title=\"Anchor for Why using mocks\">#<\/a><\/h2>\n\n\n\n<p>The following are benefits of mocks:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Speed up the test<\/li><li>Exclude external redundancies<\/li><li>Make unpredictable results predictable<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-unittest-mock-example'>Python Unittest Mock example <a href=\"#python-unittest-mock-example\" class=\"anchor\" id=\"python-unittest-mock-example\" title=\"Anchor for Python Unittest Mock example\">#<\/a><\/h2>\n\n\n\n<p>Suppose you have a module called <code>odometer.py<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> random <span class=\"hljs-keyword\">import<\/span> randint\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">speed<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> randint(<span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">120<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">alert<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    s = speed()\n    <span class=\"hljs-keyword\">if<\/span> s &lt; <span class=\"hljs-number\">60<\/span> <span class=\"hljs-keyword\">or<\/span> s &gt; <span class=\"hljs-number\">100<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the sensor.py module:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>speed()<\/code> returns the current speed of a vehicle. It returns a random value between 40 and 120. In the real world, the function would read the data from the odometer.<\/li><li>The <code><code>alert()<\/code><\/code> function returns true if the current speed is lower than 60 km\/ and higher than 120 km\/h. The <code><code>alert()<\/code><\/code> function uses the <code>speed()<\/code> function to get the current speed.<\/li><\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-mock-example.svg\" alt=\"\" class=\"wp-image-3672\"\/><\/figure>\n<\/div>\n\n\n<p>It&#8217;ll be difficult to test the <code>alert()<\/code> function because the value returned by the <code>speed()<\/code> function is varied. To resolve it, you can use <code>Mock<\/code> class.<\/p>\n\n\n\n<p>The following creates a <code>test_odometer.py<\/code> test module that tests the <code>alert()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_alert_normal (test_odometer.TestOdometer) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">1<\/span> test <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.000<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, assign a Mock object to the <code>odometer.speed<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">odometer.speed = Mock()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, set the return value of the <code>speed()<\/code> function to 70:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">odometer.speed.return_value = <span class=\"hljs-number\">70<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, call the <code><code>alert()<\/code><\/code> function and test if it returns False. The <code><code>alert()<\/code><\/code> function will call the mock object instead of the actual <code>speed()<\/code> function.<\/p>\n\n\n\n<p>The following picture illustrates how the test works with mock objects:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-mock-1.svg\" alt=\"\" class=\"wp-image-3674\"\/><\/figure>\n<\/div>\n\n\n<p>Run the test:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test_odometer.py -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_alert_normal (test_odometer.TestOdometer) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">1<\/span> test <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.000<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following adds the test cases that are over and under speed:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n<span class=\"hljs-keyword\">from<\/span> unittest.mock <span class=\"hljs-keyword\">import<\/span> Mock\n<span class=\"hljs-keyword\">import<\/span> odometer\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestOdometer<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_alert_normal<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        odometer.speed = Mock()\n        odometer.speed.return_value = <span class=\"hljs-number\">70<\/span>\n        self.assertFalse(odometer.alert())\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_alert_overspeed<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        odometer.speed = Mock()\n        odometer.speed.return_value = <span class=\"hljs-number\">100<\/span>\n        self.assertFalse(odometer.alert())\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_alert_underspeed<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        odometer.speed = Mock()\n        odometer.speed.return_value = <span class=\"hljs-number\">59<\/span>\n        self.assertTrue(odometer.alert())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Run the test:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test_odometer.py -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_alert_normal (test_odometer.TestOdometer) ... ok\ntest_alert_overspeed (test_odometer.TestOdometer) ... ok\ntest_alert_underspeed (test_odometer.TestOdometer) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">3<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.001<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>Mock<\/code> class of the <code>unittest.mock<\/code> class to mock other objects.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3655\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest-mock\/\"\n\t\t\t\tdata-post-title=\"Python Unittest Mock\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3655\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest-mock\/\"\n\t\t\t\tdata-post-title=\"Python Unittest Mock\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python unittest Mock class and how to use it to mock other classes.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3553,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3655","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=3655"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3655\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3553"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}