I am trying to test for an exception.
I have:
def test_set_catch_status_exception(self):
mro = self.mro
NEW_STATUS = 'No such status'
self.assertRaises(ValueError,mro.setStatus(NEW_STATUS))
I get the following error:
======================================================================
ERROR: test_set_catch_status_exception (__main__.TestManagementReviewGoalGetters)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_ManagementReviewObjective.py", line 68, in test_set_catch_status_exception
self.assertRaises(ValueError,mro.setStatus(NEW_STATUS))
File "/Users/eric/Dropbox/ManagementReview.py", line 277, in setStatus
raise ValueError('%s is not in the list of allowed statuses: %s' % (status,LIST_OF_STATUSES))
ValueError: No such status is not in the list of allowed statuses: ['Concern or Delay', 'On Track', 'Off Track/Needs Attention']
----------------------------------------------------------------------
Thanks
self.assertRaisesexpects a functionmro.setStatus, followed by an arbitrary number of arguments: in this case, justNEW_STATUS.self.assertRaisesassembles its arguments into the function callmro.setStatus(NEW_STATUS)inside atry...exceptblock, thus catching and recording theValueErrorif it occurs.Passing
mro.setStatus(NEW_STATUS)as an argument toself.assertRaisescauses theValueErrorto occur beforeself.assertRaisescan trap it.So the fix is to change the parentheses to a comma: