Hi, When using self.assertRaises like this you should pass a callable (the function you are going to call), but not call the function on the test. The problem is when the function takes arguments. At this point you need to create a callable with the two arguments. That can be done with functools but it's not easy to follow (If you want me to explain more on this path I can happily do it). For me the nicest syntax to test raising exceptions is to use a context manager. As the following:
with self.assertRaises(ValueError): self.cwp.getSurplusLabourTime(self.cwp.ww,self.cwp.uvc) Kind Regards, Raul On Wed, Feb 18, 2015 at 6:15 PM, Sydney Shall <s.sh...@virginmedia.com> wrote: > I use a MAC OSX 10.9.5 > Enthought Canopy Python 2.7.6 > > I am a learner. > > I am now trying to learn unittests as is often emphasised on this list. > I think that I have understood the simple unit tests such as Equal, > Greater etc. > But I am struggling with the syntax of a test for Raises an exception. > > The function that I am tring to test is: > For some reason my indentation has not been correctly copied. > I am sure that it is correct becuase I have chacked it as well as the > program. Also the 20 tests that are OK refer to all the other functions in > the program. > > def getSurplusLabourTime(self, ww, uvc): > self.ww = ww > self.uvc = uvc > try: > self.surplus_labour_time = self.ww - self.uvc > return self.surplus_labour_time > except: > if self.surplus_labour_time <= 0.0: > raise ValueError("Surplus labour time cannot be" + \ > " equal to or shorter than zero!") > > My test code is the following: > > def test_func_getSurplusLabourTime_Exc(self): > > self.assertRaises(ValueError,self.cwp.getSurplusLabourTime(self.cwp.ww,self.cwp.uvc)) > > [This last line should indented, but it refuses to do so!] > > The traceback is as follows: > > ====================================================================== > ERROR: test_func_getSurplusLabourTime_Exc (__main__.Testcwp) > ---------------------------------------------------------------------- > Traceback (most recent call last): > File > "/Users/sydney/My_Documents/Political_Economy/Capital_Simulation/Capital/Current > version/TestCWP_WorkDuration.py", line 88, in test_func_ > getSurplusLabourTime_Exc > self.assertRaises(ValueError, self.cwp.getSurplusLabourTime(self.cwp.ww, > self.cwp.uvc)) > File "/Applications/Canopy.app/appdata/canopy-1.5.1.2730. > macosx-x86_64/Canopy.app/Contents/lib/python2.7/unittest/case.py", line > 475, in assertRaises > callableObj(*args, **kwargs) > TypeError: 'float' object is not callable > > ---------------------------------------------------------------------- > Ran 21 tests in 0.005s > > FAILED (errors=1) > > > I do know that I have not added the arguments that would sause an > exception to be raised. But I have tried several forms and none have > worked. I get the same traceback as above. > > > Any guidance would be appreciated. > > > > -- > Sydney > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor