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 ...
上面的单元测试,我知道MissingInput测试类中的测试如下:
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 ...
将正确检测是否引发了 MissingInputError 异常。
有什么方法可以在测试中确定在调用异常时将哪个错误号传递给异常,以便我可以确定该错误是针对特定的缺失输入引发的,而不是针对任何其他缺失的输入?
(PS:Python 2.4.3)。
提示:如果您坚持使用 2.4 到 2.6,请使用unittest2 库。在 Python 2.7 和 3.2 中,单元测试的一大堆改进将会到来。unittest2 是用于 Python 2.4、2.5 和 2.6 的新功能(和测试)的反向移植。