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で動作する新機能(およびテスト)のバックポートです。