In unit test, how can you determine the parameter passed to an arbitrary exception?

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 ... 

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) # and so on ... 

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.

+4
source share
2 answers

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 raised MyError ('foo') or MyError (101), the test failed because they would not match the regular expression '100'. Fortunately, this method will work against numbers and everything else that you can apply to a string.

For more information about assertRaisesRegexp, see unittest documentation .

Alternatively, if you are on Python 2.6 or later, assertRaisesRegexp does not exist, and you will need to do something like this:

 try: <code> except MyError, message: self.failUnlessEqual(message.args, <expected args>) else: self.fail('MyError not raised') 
+3
source

Parameters found in the args attribute:

 >>> class CustomException(Exception): ... pass ... >>> e = CustomException(42) >>> e.args (42,) 

I would argue that this is also for Python 2.4 .

NTN

Edit : since unit tests are common code, you can also use the args argument in it:

 >>> import unittest >>> class Test(unittest.TestCase): ... def testA(self): ... try: ... raise CustomException(42) ... except CustomException, e: ... self.assertEquals(e.args[0], 42) ... >>> 
+2
source

All Articles