In a unit test, how can the parameter passed to a custom exception be determined?
Question: 
    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](http://www.voidspace.org.uk/python/modules.shtml#unittest2)**. 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.


Answer: 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](http://docs.python.org/library/unittest.html)
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')
    

