Now you can check to see if a negative number causes a
problem. Create a new function under your previous
test_area function. Name this function test_values.
(The test at the beginning is required, or unittest will
ignore the function and not check it.) You can use the
assertRaises check, which will be looking for a
ValueError exception for the function
area_of_circle, and pass it a value of -1. The following
function can be added to your code:
Click here to view code image
def test_values(self):
# Test that bad values are caught
self.assertRaises(ValueError,
area_of_circle, -1)
Example 5-9 shows the output of the test with this
additional check.
Example 5-9 Output from Adding a New Test That
Fails
Click here to view code image
.F
=========================================
FAIL: test_values
(__main__.Test_Area_of_Circle_input)
-----------------------------------------------
-----------------------
Traceback (most recent call last):
File
"/Users/chrijack/Documents/ccnadevnet/test_areacircle.py",
line 14, in
test_values
self.assertRaises(ValueError,
area_of_circle, -1)
AssertionError: ValueError not raised by
area_of_circle
-----------------------------------------------