user3156971
user3156971

Reputation: 1135

urllib2.URLError, testing python function.

I'm testing my python function

def page_data(url):
    try:
        data = urlopen(url)
    except urllib2.URLError:
        raise urllib2.URLError('bad url %s' % url)

When i pass the url 'http://faleeeeee.ru' is raisees exception(as needed) But when I run my unittest for this

self.assertRaises(page_data('http://faleeeeee.ru'), urllib2.URLError)

the test fails. Whats wrong ?

Upvotes: 0

Views: 234

Answers (1)

Mathias
Mathias

Reputation: 6839

Check Docu -> http://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaises

self.assertRaises(urllib2.URLError, page_data, 'http://faleeeeee.ru')

OR - context manager with python 2.7 and upwards.

with self.assertRaises(urllib2.URLError):
    page_data('http://faleeeeee.ru')

Upvotes: 1

Related Questions