Understand how testing and debugging complement each other.
What It Does and Why It Matters
Testing runs repeatable checks that tell you whether behavior matches expectations. Debugging is the investigation process used to locate and fix a failure once it appears. A regression test captures a bug so it will be detected automatically if it returns. Both require understanding the expected behavior before changing code.
A debugger explores execution state; an automated test records the expected behavior.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
def fee(total: float, rate: float) -> float:
return total * rate
# Regression test for a previously incorrect rate calculation
assert fee(200, 0.05) == 10
print("Regression test passed")
How the Code Works
- The definition of fee creates reusable behavior; its body runs only when called or instantiated as appropriate.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
Expected output:
Regression test passed
For the demonstrated inputs, Python displays 'Regression test passed'. This result follows from the operation described above; change one input at a time to understand which rule matters.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Start with an independently checkable requirement and test both normal and boundary inputs. A passing assertion documents only the cases that were exercised. Keep tests deterministic so a failure identifies a real change rather than environmental noise.
Practice Exercise
Deliberately change fee() to add rather than multiply, observe the test fail, then restore the function.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Fixing a bug manually without adding a regression test that would catch it next time.
- Checking only the easiest input and missing a relevant error or boundary case.
Key Takeaways
- Understand how testing and debugging complement each other.
- Testing runs repeatable checks that tell you whether behavior matches expectations.
- Deliberately change fee() to add rather than multiply, observe the test fail, then restore the function.