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

K

Ken Starks

"""
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)
 
P

Peter Otten

Ken said:
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?

(all untested)

try:
LongRational(1, "7")
except LongRationalError, e:
self.assertEquals(e.code, "2")
else:
self.fail()

Alternatively you can subclass LongRationalError...

class LongRationalError(Exception):
pass

class LongRationalDenominatorError(LongRationalError):
pass

class LongRationalNumeratorError(LongRationalError):
pass

....and then check for the specialized exception:

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

Personally, I'd probably throw a plain old TypeError for incompatible types
of both numerator and denominator.

Peter
 
K

Ken Starks

Peter said:
(all untested)

try:
LongRational(1, "7")
except LongRationalError, e:
self.assertEquals(e.code, "2")
else:
self.fail()

Alternatively you can subclass LongRationalError...

class LongRationalError(Exception):
pass

class LongRationalDenominatorError(LongRationalError):
pass

class LongRationalNumeratorError(LongRationalError):
pass

...and then check for the specialized exception:

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

Personally, I'd probably throw a plain old TypeError for incompatible types
of both numerator and denominator.

Peter

Thanks Peter, that answers my question nicely. I rather thought
I would need a try .. except structure in my unit-test itself.
 

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
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top