title: How do I assert that an HTTP exception was raised?
body: <p>I'm making an HTTP request using the <a href="https://2.python-requests.org/en/master/" rel="nofollow noreferrer">requests</a> library. If the request fails, an exception is raised. For example (truncated and simplified):</p>
  
  <p><code>main.py</code></p>
  
  <pre class="lang-py prettyprint-override"><code>from requests import get
  
  
  def make_request():
      r = get("https://httpbin.org/get")
      r.raise_for_status()
  </code></pre>
  
  <p>I've written a test using <a href="https://docs.pytest.org/en/latest/" rel="nofollow noreferrer">pytest</a> that mocks the request.</p>
  
  <p><code>test_main.py</code></p>
  
  <pre class="lang-py prettyprint-override"><code>from unittest import mock
  from unittest.mock import patch
  
  import pytest
  from requests import HTTPError
  
  from main import make_request
  
  
  @patch("main.get")
  def test_main_exception(mock_get):
      exception = HTTPError(mock.Mock(status=404), "not found")
      mock_get(mock.ANY).raise_for_status.side_effect = exception
  
      make_request()
  </code></pre>
  
  <p>However, I'm getting the following error because the exception is raised in the test—causing the test to fail.</p>
  
  <pre><code>$ pytest
  ...
  E               requests.exceptions.HTTPError: [Errno &lt;Mock id='4352275232'&gt;] not found
  
  /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1011: HTTPError
  ==================================================== 1 failed in 0.52s ====================================================
  </code></pre>
  
  <p>How do I assert on the behavior that occurs when an HTTP exception is raised (e.g. 404 status code)?</p>