How do I unit-test a specific case of a home-rolled exception ?

Ken Starks straton at lampsacos.demon.co.uk
Fri Jun 27 06:37:37 EDT 2008


"""
I'm a bit new to both home-made exceptions and unit
tests, so sorry if I'm just being stupid or doing it
totally wrong.

I have an exception class, and I want to check that
a particular instance of it has been raised; or
more accurately that one is raised that is equal to
an instance I specify.

In the example below, I can check that a
'LongRationalError' is raised, but I want
to check that it is specifically a
'LongrationalError('1') or a 'LongRationalError('2')

How do I do that?

+++++++++++++++++++++++++++++++++

The critical line is:

         self.assertRaises(LongRationalError, LongRational,  1, "7")

and it doesn't work if i put:


         self.assertRaises(LongRationalError('2'), LongRational,  1, "7")


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

"""

import unittest

class LongRationalError(Exception):
     Message={}
     Message['1']="numerator must be an integer"
     Message['2']="denominator must be an integer"
     def __init__(self, code):
         self.code = str(code)
     def __str__(self):
         k = self.code
         if k in self.Message.keys():
             v = self.Message[k]
         else:
             v = '...no description provided'
         return "Long Rational error #%s: %s" % (k,v)


class LongRational():
     import types
     allowed = [types.IntType,types.LongType]

     def __init__(self,a,b):
         if type(a) not in self.allowed:
             raise LongRationalError("1")
         if type(b) not in self.allowed:
             raise LongRationalError("2")
         if b == 0:
             raise ValueError("supplied denominator must be non-zero")
         if a == 0:
             b = 1
         self.numerator=long(a)
         self.denominator=long(b)


class TestLongRationals(unittest.TestCase):


     def test_init(self):
         # if the supplied denominator ==  zero, we should get a ValueError
         # if the supplied numerator or denominator is anything but an 
integer or a
         # long we should
         # raise a LongRational exception
         from types import LongType
         e = LongRational(123,456)
         self.assertRaises(ValueError, LongRational,  1, 0)
         self.assertRaises(LongRationalError, LongRational,  1.0, 2)
         self.assertRaises(LongRationalError, LongRational,  3, 4.0)
         self.assertRaises(LongRationalError, LongRational,  1, "7")
         self.assertEquals(e.numerator,123)
         self.assertEquals(e.denominator,456L)
         self.assertEquals(type(e.numerator),LongType)
         self.assertEquals(type(e.denominator),LongType)
         # special case: denominator of zero rational forced to unity.
         self.assertEquals(LongRational(0,24).denominator, 1L)



if __name__ == '__main__':
     suite = unittest.TestLoader().loadTestsFromTestCase(TestLongRationals)
     unittest.TextTestRunner(verbosity=2).run(suite)






More information about the Python-list mailing list