10.4 Looking for orphan code
You have not thoroughly tested your code if you have not executed each line at least once. Checking for this is called coverage testing, and coverage is the tool to use. Install coverage from the operating system command line via
pip install coverage
Suppose I have the function plane_location, which accepts two arguments: the x coordinate of a point in the plane and the y coordinate. The function prints whether the point is
- at the origin (0, 0)
- on the x-axis (if y = 0)
- on the y-axis (if x = 0)
- in the first quadrant (if x and y > 0)
- in the second quadrant (if x < 0 and y > 0)
- in the third quadrant (if x < 0 and y < 0)
- in the fourth quadrant (if x > 0 and y < 0)
The implementation is straightforward, and I follow it with some examples:
def plane_location(x, y):
if x == 0:
if y == 0:
...