unittest issues

P

paul koelle

Hi,

I try to use the "failUnlessRaises()" method from unittest.TestCase as
follows:

where IntegrityError is an Exception somewhere deep inside pysqlite.
Running it spits out:

Do I really need to tell unittest about each and every possible
Exception it might catch. Whats going on here?

python 2.2.3 on linux 2.6.7

thanks
Paul
 
A

Alex Martelli

paul koelle said:
Hi,

I try to use the "failUnlessRaises()" method from unittest.TestCase as
follows:


where IntegrityError is an Exception somewhere deep inside pysqlite.
Running it spits out:


Do I really need to tell unittest about each and every possible
Exception it might catch. Whats going on here?

You need to define any name you use before you use it, quite apart from
unittest. You can't use a name that just isn't defined in that scope,
period: whether you're unit testing or doing anything else, you can't.

Once you have fixed this you'll find another problem: you need to pass
failUnlessRaises the callable and arguments separately, so it can
perform the call within a try clause. What you're doing here is calling
the myObject.new method yourself, so it presumably raises well before
any kind of control is passed to the failUnlessRaises method -- no good.
I'm not sure you can pass an arbitrary set of keywords that way (at the
very least some keyword might happen to conflict with ones used in
unittest itself), so I suggest this approach:

def localfun(): return myObject.new(**stuff)
self.failUnlessRaises(Exception, localfun)

note that here I'm passing the function object itself, NOT the result of
calling it -- there IS quite a difference!


Alex
 

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

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top