{"id":3616,"date":"2022-07-03T08:52:20","date_gmt":"2022-07-03T08:52:20","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3616"},"modified":"2022-07-09T07:46:55","modified_gmt":"2022-07-09T07:46:55","slug":"python-mock-requests","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-mock-requests\/","title":{"rendered":"Python Mock Requests"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to mock the <code>requests<\/code> module in Python to test an API call using the <a href=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest\/\">unittest<\/a> module.<\/p>\n\n\n\n<p>The <code>requests<\/code> module is an HTTP library that allows you to send HTTP requests easily. Typically, you use the <code>requests<\/code> module to call an API from a remote server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-scenario'>The scenario <a href=\"#the-scenario\" class=\"anchor\" id=\"the-scenario\" title=\"Anchor for The scenario\">#<\/a><\/h2>\n\n\n\n<p>For the demo purposes, we&#8217;ll use a public API provided by <a href=\"https:\/\/jsonplaceholder.typicode.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">jsonplaceholder.typicode.com<\/a>:<\/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\">https:&#47;&#47;jsonplaceholder.typicode.com\/<\/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>To make an API call, you&#8217;ll use the <code>requests<\/code> method to send an HTTP GET method to the following end-point:<\/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\">https:\/\/jsonplaceholder.typicode.com\/albums\/<span class=\"hljs-number\">1<\/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>It&#8217;ll return JSON data in the following format:<\/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\">{\n  <span class=\"hljs-string\">\"userId\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n  <span class=\"hljs-string\">\"id\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n  <span class=\"hljs-string\">\"title\"<\/span>: <span class=\"hljs-string\">\"quidem molestiae enim\"<\/span>\n}<\/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>Since the <code>requests<\/code> module is not a built-in module, you need to install it by running the following pip command:<\/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\">pip install requests<\/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<h2 class=\"wp-block-heading\" id='making-an-api-call-using-the-requests-module'>Making an API call using the requests module <a href=\"#making-an-api-call-using-the-requests-module\" class=\"anchor\" id=\"making-an-api-call-using-the-requests-module\" title=\"Anchor for Making an API call using the requests module\">#<\/a><\/h2>\n\n\n\n<p>The following defines a new module called <code>album.py<\/code> with a function <code>find_album_by_id()<\/code> that returns an album by an id:<\/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\"><span class=\"hljs-keyword\">import<\/span> requests\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">find_album_by_id<\/span><span class=\"hljs-params\">(id)<\/span>:<\/span>\n    url = <span class=\"hljs-string\">f'https:\/\/jsonplaceholder.typicode.com\/albums\/<span class=\"hljs-subst\">{id}<\/span>'<\/span>\n    response = requests.get(url)\n    <span class=\"hljs-keyword\">if<\/span> response.status_code == <span class=\"hljs-number\">200<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> response.json()&#91;<span class=\"hljs-string\">'title'<\/span>]\n    <span class=\"hljs-keyword\">else<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">None<\/span><\/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>How it works.<\/p>\n\n\n\n<p>First, format the API end-point that includes the id parameter:<\/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\">url = <span class=\"hljs-string\">f'https:\/\/jsonplaceholder.typicode.com\/albums\/<span class=\"hljs-subst\">{id}<\/span>'<\/span><\/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>Second, call the <code>get()<\/code> function of the requests module to get a <code>Response<\/code> object:<\/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\">response = requests.get(url)<\/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>Third, call the <code>json()<\/code> method of the response object if the API call succeeds:<\/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\"><span class=\"hljs-keyword\">if<\/span> response.status_code == <span class=\"hljs-number\">200<\/span>:\n   <span class=\"hljs-keyword\">return<\/span> response.json()&#91;<span class=\"hljs-string\">'title'<\/span>]\n<span class=\"hljs-keyword\">else<\/span>:\n   <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">None<\/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 <code>response.json()<\/code> returns a dictionary that represents the JSON data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-test-module'>Creating a test module <a href=\"#creating-a-test-module\" class=\"anchor\" id=\"creating-a-test-module\" title=\"Anchor for Creating a test module\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll create a <code>test_album.py<\/code> test module that tests the functions in the <code>album.py<\/code> module:<\/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\">import<\/span> unittest\n\n<span class=\"hljs-keyword\">from<\/span> album <span class=\"hljs-keyword\">import<\/span> find_album_by_id\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestAlbum<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n   <span class=\"hljs-keyword\">pass<\/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<h3 class=\"wp-block-heading\" id='mocking-the-requests-module'>Mocking the requests module <a href=\"#mocking-the-requests-module\" class=\"anchor\" id=\"mocking-the-requests-module\" title=\"Anchor for Mocking the requests module\">#<\/a><\/h3>\n\n\n\n<p>The <code>find_album_by_id()<\/code> function has two dependencies: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>get()<\/code> method of the <code>requests<\/code> module <\/li><li>The <code>Response<\/code> object returned by the <code>get()<\/code> function.<\/li><\/ul>\n\n\n\n<p>So to test the <code>find_album_by_id()<\/code> function, you need to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, mock the requests module and call the <code>get()<\/code> function (<code>mock_requests<\/code>)<\/li><li>Second, mock the returned response object.<\/li><\/ul>\n\n\n\n<p>In other words, the mock_requests.<code>get()<\/code> returns a mock response object.<\/p>\n\n\n\n<p>To mock the requests module, you can use the <code>patch()<\/code> function. Suppose that the <code>mock_requests<\/code> is a mock of the <code>requests<\/code> module.<\/p>\n\n\n\n<p>The <code>mock_requests.get()<\/code> should return a mock for the response. To mock the response, you can use the <code>MagicMock<\/code> class of the <code>unittest.mock<\/code> module.<\/p>\n\n\n\n<p>The following shows how to test the <code>find_album_by_id()<\/code> using the <code>test_find_album_by_id_success()<\/code> test method:<\/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\"><span class=\"hljs-keyword\">import<\/span> unittest\n<span class=\"hljs-keyword\">from<\/span> unittest.mock <span class=\"hljs-keyword\">import<\/span> MagicMock, patch\n\n<span class=\"hljs-keyword\">from<\/span> album <span class=\"hljs-keyword\">import<\/span> find_album_by_id\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestAlbum<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n\n<span class=\"hljs-meta\">    @patch('album.requests')<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_find_album_by_id_success<\/span><span class=\"hljs-params\">(self, mock_requests)<\/span>:<\/span>\n        <span class=\"hljs-comment\"># mock the response<\/span>\n        mock_response = MagicMock()\n        mock_response.status_code = <span class=\"hljs-number\">200<\/span>\n        mock_response.json.return_value = {\n            <span class=\"hljs-string\">'userId'<\/span>: <span class=\"hljs-number\">1<\/span>,\n            <span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>,\n            <span class=\"hljs-string\">'title'<\/span>: <span class=\"hljs-string\">'hello'<\/span>,\n        }\n\n        <span class=\"hljs-comment\"># specify the return value of the get() method<\/span>\n        mock_requests.get.return_value = mock_response\n\n        <span class=\"hljs-comment\"># call the find_album_by_id and test if the title is 'hello'<\/span>\n        self.assertEqual(find_album_by_id(<span class=\"hljs-number\">1<\/span>), <span class=\"hljs-string\">'hello'<\/span>)<\/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, patch the <code>requests<\/code> module as the <code>mock_requests<\/code> object:<\/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\"><span class=\"hljs-meta\">@patch('album.requests')<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_find_album_by_id_success<\/span><span class=\"hljs-params\">(self, mock_requests)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># ...<\/span><\/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, mock the response of the <code>get()<\/code> function using the <code>MagicMock<\/code> class. In this test method, we specify the status code 200 and <code>return_value<\/code> of the <code>json()<\/code> function as a hard-coded value:<\/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\">mock_response = MagicMock()\nmock_response.status_code = <span class=\"hljs-number\">200<\/span>\nmock_response.json.return_value = {\n   <span class=\"hljs-string\">'userId'<\/span>: <span class=\"hljs-number\">1<\/span>,\n   <span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>,\n   <span class=\"hljs-string\">'title'<\/span>: <span class=\"hljs-string\">'hello'<\/span>,\n}<\/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, use the mock_response as the return value of the <code>get()<\/code> function:<\/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\"> mock_requests.get.return_value = mock_response<\/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>Finally, test if the title of the album is equal to the one that we specified in the <code>return_value<\/code> of the <code>mock_response<\/code>:<\/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\">self.assertEqual(find_album_by_id(<span class=\"hljs-number\">1<\/span>), <span class=\"hljs-string\">'hello'<\/span>)<\/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>Run the test:<\/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\">python -m unittest -v<\/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>Output:<\/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\">test_find_album_by_id_success (test_album.TestAlbum) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">1<\/span> test <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-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>By using the same technique, you can also test the <code>find_album_by_id()<\/code> function in the failed case:<\/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\"><span class=\"hljs-keyword\">import<\/span> unittest\n<span class=\"hljs-keyword\">from<\/span> unittest.mock <span class=\"hljs-keyword\">import<\/span> MagicMock, patch\n\n<span class=\"hljs-keyword\">from<\/span> album <span class=\"hljs-keyword\">import<\/span> find_album_by_id\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestAlbum<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n\n<span class=\"hljs-meta\">    @patch('album.requests')<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_find_album_by_id_success<\/span><span class=\"hljs-params\">(self, mock_requests)<\/span>:<\/span>\n        <span class=\"hljs-comment\"># mock the response<\/span>\n        mock_response = MagicMock()\n        mock_response.status_code = <span class=\"hljs-number\">200<\/span>\n        mock_response.json.return_value = {\n            <span class=\"hljs-string\">'userId'<\/span>: <span class=\"hljs-number\">1<\/span>,\n            <span class=\"hljs-string\">'id'<\/span>: <span class=\"hljs-number\">1<\/span>,\n            <span class=\"hljs-string\">'title'<\/span>: <span class=\"hljs-string\">'hello'<\/span>,\n        }\n\n        <span class=\"hljs-comment\"># specify the return value of the get() method<\/span>\n        mock_requests.get.return_value = mock_response\n\n        <span class=\"hljs-comment\"># call the find_album_by_id and test if the title is 'hello'<\/span>\n        self.assertEqual(find_album_by_id(<span class=\"hljs-number\">1<\/span>), <span class=\"hljs-string\">'hello'<\/span>)\n\n<span class=\"hljs-meta\">    @patch('album.requests')<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_find_album_by_id_fail<\/span><span class=\"hljs-params\">(self, mock_requests)<\/span>:<\/span>\n        mock_response = MagicMock()\n        mock_response.status_code = <span class=\"hljs-number\">400<\/span>\n\n        mock_requests.get.return_value = mock_response\n        self.assertIsNone(find_album_by_id(<span class=\"hljs-number\">1<\/span>))<\/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<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_find_album_by_id_fail (test_album.TestAlbum) ... ok\ntest_find_album_by_id_success (test_album.TestAlbum) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">2<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.002<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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>patch()<\/code> function to mock the requests module (<code>mock_requests<\/code>)<\/li><li>Use the <code>MagicMock<\/code> to mock the response returned by the <code>mock_requests.get()<\/code> function.<\/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=\"3616\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-mock-requests\/\"\n\t\t\t\tdata-post-title=\"Python Mock Requests\"\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=\"3616\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-mock-requests\/\"\n\t\t\t\tdata-post-title=\"Python Mock Requests\"\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 how to mock API call that uses the requests module.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3553,"menu_order":15,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3616","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3616","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=3616"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3616\/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=3616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}