Re: [Tutor] unittest for: Raises an exception
On 18/02/2015 20:40, Ben Finney wrote: Sydney Shall writes: 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!] What is “it” which refuses to indent your text? You might need to use a better message composition tool. If you're typing into a Web application, please see the discussion happening in this forum about appropriate email clients for posting program code. So I'll reformat that code for readability:: def test_func_getSurplusLabourTime_Exc(self): self.assertRaises( ValueError, self.cwp.getSurplusLabourTime(self.cwp.ww, self.cwp.uvc)) The traceback is as follows: == ERROR: test_func_getSurplusLabourTime_Exc (__main__.Testcwp) -- Traceback (most recent call last): […] "/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 The error message is correct: the ‘assertRaises’ method expects a callable object in the second parameter, but you've supplied an object which is not callable (a float). Have a closer look at the documentation for ‘TestCase.assertRaises’:: assertRaises(exception, callable, *args, **kwds) Test that an exception is raised when callable is called with any positional or keyword arguments that are also passed to assertRaises(). https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises> It's unfortunate the documentation doesn't give an example. But note that clause “when `callable` is called with any […] arguments that *are also passed to assertRaises*”. So you don't call the function yourself; if you do, you get back its return value (in your case, a float object) and *that's* your argument to ‘assertRaises’ — not the function you're trying to test! Instead of calling the function and getting its return value, you pass *the function itself* to ‘assertRaises’, along with any arguments you want *the test method* to call it with. This indirection is a little confusing, and again I'm unhappy the documentation doesn't show an example. Here's one:: def test_func_getSurplusLabourTime_Exc(self): self.assertRaises( ValueError, self.cwp.getSurplusLabourTime, self.cwp.ww, self.cwp.uvc) What the documentation does show as an example, though, is a new-ish feature that might suit you better. You can now make your test code more comprehensible by instead using the ‘assertRaises’ method as a context manager, and then you just call your function normally. Like this:: def test_func_getSurplusLabourTime_Exc(self): with self.assertRaises(ValueError): self.cwp.getSurplusLabourTime(self.cwp.ww, self.cwp.uvc) Context managers are a very helpful feature that can make code more elegant and readable. They might seem a little magic for now if you haven't learned about them yet, but this is a good demonstration of the improvement they can make. Raul and Ben, Many thanks for the advice. The use of this test is much clearer to me now, and I think it is now working with your help. But I will practice my use of it. As for my formatting, this problem of indentation has not occurred before. I use Thunderbird and Firefox and I have not had this problem up to now. I shall examine the problem and see what is wrong and correct it. Thanks, Sydney -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unittest for: Raises an exception
Sydney Shall writes: > 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!] What is “it” which refuses to indent your text? You might need to use a better message composition tool. If you're typing into a Web application, please see the discussion happening in this forum about appropriate email clients for posting program code. So I'll reformat that code for readability:: def test_func_getSurplusLabourTime_Exc(self): self.assertRaises( ValueError, self.cwp.getSurplusLabourTime(self.cwp.ww, self.cwp.uvc)) > The traceback is as follows: > > == > ERROR: test_func_getSurplusLabourTime_Exc (__main__.Testcwp) > -- > Traceback (most recent call last): […] > "/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 The error message is correct: the ‘assertRaises’ method expects a callable object in the second parameter, but you've supplied an object which is not callable (a float). Have a closer look at the documentation for ‘TestCase.assertRaises’:: assertRaises(exception, callable, *args, **kwds) Test that an exception is raised when callable is called with any positional or keyword arguments that are also passed to assertRaises(). https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises> It's unfortunate the documentation doesn't give an example. But note that clause “when `callable` is called with any […] arguments that *are also passed to assertRaises*”. So you don't call the function yourself; if you do, you get back its return value (in your case, a float object) and *that's* your argument to ‘assertRaises’ — not the function you're trying to test! Instead of calling the function and getting its return value, you pass *the function itself* to ‘assertRaises’, along with any arguments you want *the test method* to call it with. This indirection is a little confusing, and again I'm unhappy the documentation doesn't show an example. Here's one:: def test_func_getSurplusLabourTime_Exc(self): self.assertRaises( ValueError, self.cwp.getSurplusLabourTime, self.cwp.ww, self.cwp.uvc) What the documentation does show as an example, though, is a new-ish feature that might suit you better. You can now make your test code more comprehensible by instead using the ‘assertRaises’ method as a context manager, and then you just call your function normally. Like this:: def test_func_getSurplusLabourTime_Exc(self): with self.assertRaises(ValueError): self.cwp.getSurplusLabourTime(self.cwp.ww, self.cwp.uvc) Context managers are a very helpful feature that can make code more elegant and readable. They might seem a little magic for now if you haven't learned about them yet, but this is a good demonstration of the improvement they can make. -- \ “… one of the main causes of the fall of the Roman Empire was | `\that, lacking zero, they had no way to indicate successful | _o__) termination of their C programs.” —Robert Firth | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unittest for: Raises an exception
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 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
[Tutor] unittest for: Raises an exception
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