Sean W.
Sean W.

Reputation: 5142

assertRises failure

I'm trying to write a unit test that will ensure an HTTPException is raised when necessary. Here is the test:

import unittest  
from requests import HTTPError
import pyport

 # Code omitted...
 def test_bad_item_type(self):
     """A bad item type should raise a HTTPError"""
     test_type = 'bad'
     test_id = 1986134
     self.assertRaises(HTTPError, pyport.get_item(test_type, test_id))

Which produces the following:

ERROR: test_bad_item_type (__main__.TestPyportFunctions) A bad item
type should raise requests.HTTPError
---------------------------------------------------------------------- 
Traceback (most   recent call last):   File "./tests.py", line 65, in
test_bad_item_type
    self.assertRaises(HTTPError, pyport.get_item(test_type, test_id))   File    "/home/sean/workspace/pyport/pyport.py", line 54, in get_item
    response.raise_for_status()   File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 741, fin raise_for_status
    raise HTTPError('%s Client Error' % self.status_code) HTTPError: 404 Client Error

The exception is raised, but it is not caught by the test. This is similar to what happened in this question, but it is not quite the same. Can someone tell me what I'm missing?

Upvotes: 6

Views: 1758

Answers (2)

gps
gps

Reputation: 1390

If you are using Python 2.7 or later or you are using the backported unittest2 module instead of unittest, you can avoid having to write strange syntax when using assertRaises by using it as a context manager:

with self.assertRaises(HTTPError):
    pyport.get_item(test_type, test_id)

http://docs.python.org/library/unittest.html#basic-example

Upvotes: 2

Rob Wouters
Rob Wouters

Reputation: 16327

It should be:

self.assertRaises(HTTPError, pyport.get_item, test_type, test_id)

See the signature of assertRaises:

assertRaises(exception, callable, *args, **kwds)

This is defined this way because if you do it your way, the Python interpreter first calls pyport.get_item(test_type, test_id) and then passes its result to assertRaises. The result being assertRaises is not called at all and the exception is not caught. Now if assertRaises has access to the function and its arguments it can call the function itself and catch the appropriate exception.

Upvotes: 7

Related Questions