Reputation: 981
I have the following code and wanted to untitest when the given function raises for "FileNotFoundError
def get_token():
try:
auth = get_auth() # This function returns auth ,if file exists else throws "FileNotFoundError
except FileNotFoundError:
auth= create_auth()
return auth
I am having trouble figuring out how to test the condition where it raises "FileNotFoundError" and doesn't call create_auth.
Any hint would be appreciated
Thanks
Upvotes: 4
Views: 1735
Reputation: 122376
In your unit test you'd need to mock the get_auth
function and cause it to raise a FileNotFoundError
by using the .side_effect
attribute:
@mock.patch('path.to.my.file.get_auth')
def test_my_test(self, mock_get_auth):
mock_get_auth.side_effect = FileNotFoundError
You can then test whether create_auth
was actually called:
@mock.patch('path.to.my.file.create_auth')
@mock.patch('path.to.my.file.get_auth')
def test_my_test(self, mock_get_auth, mock_create_auth):
mock_get_auth.side_effect = FileNotFoundError
get_token()
self.assertTrue(mock_create_auth.called)
Upvotes: 4