Mocking a function to raise an Exception to test an except block
I have a function ( foo ) which calls another function ( bar ). If invoking bar() raises an HttpError , I want to handle it specially if the status code is 404, otherwise re-raise. I am trying to write some unit tests around this foo function, mocking out the call to bar() . Unfortunately, I am unable to get the mocked call to bar() to raise an Exception which is caught by my except block. Here is my code which illustrates my problem: import unittest import mock from apiclient.errors import HttpError class FooTests(unittest.TestCase): @mock.patch('my_tests.bar') def test_foo