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."}
...
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)
Testing the module above, I know that the following tests are 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)
will correctly determine if a MissingInputError exception has been thrown.
Is there a way to determine in the test which error number was passed to the exception when it was called, so that I can be sure that the error occurs for this missing input, and not for any other missing inputs?
(PS: 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, there will come a whole bunch of improvements for unittest. unittest2 is the backport of new features (and tests) for working with Python 2.4, 2.5, and 2.6.
source share