Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unittest assertRaises Problem

Reply
Thread Tools

unittest assertRaises Problem

 
 
john
Guest
Posts: n/a
 
      04-16-2007
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

 
Reply With Quote
 
 
 
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      04-16-2007
On Apr 16, 3:13 pm, "john" <(E-Mail Removed)> wrote:
> 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)

--
Hope this helps,
Steven

 
Reply With Quote
 
 
 
 
john
Guest
Posts: n/a
 
      04-17-2007
On Apr 16, 6:35 pm, Jean-Paul Calderone <(E-Mail Removed)> wrote:
> On 16 Apr 2007 15:13:42 -0700, john <(E-Mail Removed)> wrote:
>
>
>
> >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 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: assertraises behaviour andrea crotti Python 0 07-16-2012 04:38 PM
unittest: assertRaises() with an instance instead of a type Ulrich Eckhardt Python 20 04-02-2012 07:42 AM
Case study: debugging failed assertRaises bug Steven D'Aprano Python 2 04-27-2011 02:54 PM
Interaction btw unittest.assertRaises and __getattr__. Bug? Inyeol Python 3 10-27-2010 07:15 AM
Unittest - adding a doctest suite to unittest.main Paul Moore Python 1 10-14-2008 03:32 PM



Advertisments