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.
You can pass a regular expression that runs against the message:
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: