title: Mock exception with side effect raised in class method and caught in calling method gives 'did not raise'
body: <p>Using <a href="https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock" rel="nofollow noreferrer">side_effect</a>, I am trying to raise an exception when a mock is called but I get a <code>DID NOT RAISE EXCEPTION</code> error that I do not understand.</p>
  
  <p>Based largely on this <a href="https://stackoverflow.com/a/50990039/3286832">answer</a>, I have created a simple example where there is a <code>Query</code> class with a class method <code>make_request_and_get_response</code> which can raise several exceptions. These exceptions are being handled within the <code>get_response_from_external_api</code> method in <code>main.py</code>.</p>
  
  <p>query.py</p>
  
  <pre class="lang-py prettyprint-override"><code>from urllib.request import urlopen
  import contextlib
  import urllib
  
  class Query:
      def __init__(self, a, b):
          self.a = a
          self.b = b
  
          self.query = self.make_query()
  
  
      def make_query(self):
          # create query request using self.a and self.b
          return query
  
  
      def make_request_and_get_response(self):  # &lt;--- the 'dangerous' method that can raise exceptions
          with contextlib.closing(urlopen(self.query)) as response:
              return response.read().decode('utf-8')
  </code></pre>
  
  <p>main.py</p>
  
  <pre class="lang-py prettyprint-override"><code>from foo.query import *
  
  def get_response_from_external_api(query):
      try:
          response = query.make_request_and_get_response()
      except urllib.error.URLError as e:
          print('Got a URLError: ', e)
      except urllib.error.HTTPError as e:
          print('Got a HTTPError: ', e)
      # {{various other exceptions}}
      except Exception:
          print('Got a generic Exception!')
          # handle this exception
  
  
  if __name__ == "__main__":    
      query = Query('input A', 'input B')
      result = get_response_from_external_api(query)
      return result
  
  </code></pre>
  
  <p>Using <code>pytest</code>, I am trying to mock that 'dangerous' method (<code>make_request_and_get_response</code>) with a side effect for a specific exception. Then, I proceed with creating a mocked <code>Query</code> object to use when calling the <code>make_request_and_get_response</code> with the expectation that this last call give a 'URLError' exception.</p>
  
  <p>test_main.py</p>
  
  <pre class="lang-py prettyprint-override"><code>import pytest
  from unittest.mock import patch
  from foo.query import Query
  from foo.main import get_response_from_external_api
  
  
  class TestExternalApiCall:
      @patch('foo.query.Query')
      def test_url_error(self, mockedQuery):
          with patch('foo.query.Query.make_request_and_get_response', side_effect=Exception('URLError')):
              with pytest.raises(Exception) as excinfo:
                  q= mockedQuery()
                  foo.main.get_response_from_external_api(q)
              assert excinfo.value = 'URLError'
              # assert excinfo.value.message == 'URLError' # this gives object has no attribute 'message'
  </code></pre>
  
  <p>The test above gives the following error:</p>
  
  <pre><code>&gt;       foo.main.get_response_from_external_api(q)
  E       Failed: DID NOT RAISE &lt;class 'Exception'&gt; id='72517784'&gt;") == 'URLError'
  </code></pre>