unittest assertRaises Problem

J

john

All:

Hi. I am an experienced developer (15 yrs), but new to Python and have
a question re unittest and assertRaises. No matter what I raise,
assertRaises is never successful. Here is the test code:


class Foo:
def testException(self):
raise ValueError

class FooTestCase(unittest.TestCase):

testTryThis(self):
f = Foo()
self.assertRaises(ValueError, f.testException())

This fails --- unittest reports the following:
FAILED (errors=1)

This seems like the most basic thing in the world. I am running Python
2.5 on Windows XP using Eclipse and PyDev

Any help appreciated.

Thanks,
John
 
A

attn.steven.kuo

All:

Hi. I am an experienced developer (15 yrs), but new to Python and have
a question re unittest and assertRaises. No matter what I raise,
assertRaises is never successful. Here is the test code:

class Foo:
def testException(self):
raise ValueError

class FooTestCase(unittest.TestCase):

testTryThis(self):
f = Foo()
self.assertRaises(ValueError, f.testException())


The second argument should be a callable object, not the
result from invoking that callable object. Thus, change

self.assertRaises(ValueError, f.testException())

to

self.assertRaises(ValueError, f.testException)
 
J

john

The 2nd argument to assertRaises should be a callable. assertRaises
will call it (so that it can do exception handling), so you shouldn't:

self.assertRaises(ValueError, f.testException)

Jean-Paul

Steven, Jean-Paul:

Thank you both for your answers - worked like a charm!

Best,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top