Notes on unittest.py

Thomas Heller thomas.heller at ion-tof.com
Mon Oct 23 07:32:37 EDT 2000


> This looks interesting...  I've been using PyUnit a lot.  It's quite good
> (very solid in my experience), but exceptions are a pain to test for.  I
> generally end up doing something like this to beat the right results out of
> it:
> 
> def check_something(self):
>     val = self.mything(1) # this should pass
>     assert val == 1, 'mything was %s' % val
> 
> def check_something_exception(self):
>     try:
>         val = self.mything(-1) # this should raise an exception
>     except TheExceptionIThinkItWillRaise:
>         pass
>     else:
>         assert 1 == 2, ('check_something_exception did not '
>                         'raise proper exception on argument %s' % val)
> 
You could also use the assertRaises() method in pyunit:

def check_something_exception(self):
    self.assertRaises(TheExceptionIThinkItWillRaise, "self.mything", (-1,))

Thomas





More information about the Python-list mailing list