4
class AppError(Exception): pass

class MissingInputError(AppError):

    em = {1101: "Date input is missing. Please verify.", \
          1102: "Key input is missing. Please verify.", \
          1103: "Stn input is missing. Please verify."}
          # and so on ...

...

def validate(self):
    """ Method of Input class to validate input and save it """

    params = self.__params

    if 'dt' in params:
        self.__validateKey(escape(params['dt'][0]))
    else:
        raise MissingInputError(1101)

    if 'key' in params:
        self.__validateService(escape(params['key'][0]))
    else:
        raise MissingInputError(1102)

    # and so on ...

Unit testing the above, I know that the following tests in the MissingInput test class:

def testMissingKeyInput(self):
    """ Missing key should raise error """
    ip = controller.Input(MissingInput.missInputKey)
    self.assertRaises(errors.MissingInputError, ip.validate)

def testMissingDtInput(self):
    """ Missing dt should raise error """
    ip = controller.Input(MissingInput.missInputDt)
    self.assertRaises(errors.MissingInputError, ip.validate)

# and so on ...

will correctly detect if a MissingInputError exception was raised.

Is there any way to determine in the test what error number was passed to the exception while calling it, so that I can be sure that the error is being raised for that particular missing input, and not for any other missing inputs?

(P.S: Python 2.4.3).


Tip: If you are stuck with 2.4 to 2.6, use the unittest2 library. In Python 2.7 and 3.2 a whole bunch of improvements to unittest will arrive. unittest2 is a backport of the new features (and tests) to work with Python 2.4, 2.5 & 2.6.

Sam
  • 1,358
  • 4
  • 16
  • 30

2 Answers2

3

You can pass a regular expression that runs against the message:

import unittest

class MyError(Exception):
    pass

def raiseError():
    raise MyError(100)

class TestStuff(unittest.TestCase):
    def testError(self):
        self.assertRaisesRegexp(MyError, '100', raiseError)

unittest.main()    

Does that make sense to you? If you were raising MyError('foo') or MyError(101), the test would fail because those wouldn't match the regular expression of '100'. Fortunately, this method will work against numbers and anything else that you can cast to a string.

See the unittest documentation for details on assertRaisesRegexp.

Alternatively, if you're on Python 2.6 or older, assertRaisesRegexp is not there and you'll have to do something like this:

try:
    <code>
except MyError, message:
    self.failUnlessEqual(message.args, <expected args>)
else:
    self.fail('MyError not raised')
Ken Kinder
  • 12,654
  • 6
  • 50
  • 70
  • Thanks, Ken. Unfortunately, I get the following error - AttributeError: 'TestStuff' object has no attribute 'assertRaisesRegexp' - guess Python 2.4.3 doesn't support this. – Sam Jun 01 '11 at 19:21
  • Yeah, 2.4 is pretty old. In that case, you'll probably want to just do a try/except block. I'll update my answer. – Ken Kinder Jun 01 '11 at 19:30
  • In fact, it is just present in Python 2.7 or later. – brandizzi Jun 01 '11 at 19:31
  • Yeah, but RedHat / CentOS prefer that version of Python and so I am stuck with it. :) P.S: I think you meant, `self.failUnlessEqual(message.args[0], )`. – Sam Jun 01 '11 at 20:06
  • Ah... You might be able just go get the unittest module from 2.7, but I doubt that's worth it. And you're right about message.args; I updated my answer. – Ken Kinder Jun 01 '11 at 20:31
2

The parameters are found in the args attribute:

>>> class CustomException(Exception):
...     pass
... 
>>> e = CustomException(42)
>>> e.args
(42,)

I'd bet it is available for Python 2.4 as well.

HTH

Edit: since unit tests is common code, you can use the args argument in it as well:

>>> import unittest
>>> class Test(unittest.TestCase):
...     def testA(self):
...         try:
...             raise CustomException(42)
...         except CustomException, e:
...             self.assertEquals(e.args[0], 42)
... 
>>> 
brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • Thanks. Actually, that's exactly how I've been catching my exceptions. But somehow, I had this nagging (irrational?) feeling that a try...except block shouldn't be used in unittest. – Sam Jun 01 '11 at 19:56
  • Oh, no, there is no such constraint. If you were using Python 2.7 I'd recommend you to use the `assertRaises()` method, as Ken used, but in Python 2.4 a `try except` is the way to go. Anyway, the `args` argument is available in the exception using any of these solutions. – brandizzi Jun 01 '11 at 20:01