[Tutor] unittest for: Raises an exception
Sydney Shall
s.shall at virginmedia.com
Wed Feb 18 19:15:05 CET 2015
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
More information about the Tutor
mailing list