Testing states what should happen; debugging investigates why something else happened. Use both together by reducing a failure to a small example, fixing its cause and keeping a meaningful regression check.
Write down the promised behaviour
A useful test has an input, an observable result and a reason that result matters. For a price calculation, zero quantity and an ordinary positive quantity are different meaningful cases. A test that merely repeats the same implementation expression can reproduce the same mistake instead of detecting it.
Reduce the failure before changing code
Record the exact input and error. Reproduce the problem in a controlled environment, then remove unrelated steps until the cause becomes easier to see. Check one hypothesis at a time using logs, a debugger or a small experiment. Avoid exposing credentials or personal information while gathering evidence.
Keep the regression focused
Once the cause is known, add a test that would have caught that failure and verify the fix. Broader tests may be appropriate when the change crosses system boundaries. A unit test of a calculation cannot establish that a browser form, network request and database transaction all work together.
Worked example
Two small checks describe the behaviour of total_price. A learner can introduce a deliberate multiplication bug, run the tests and read the failure before repairing the function. The printed message appears only after both checks succeed.
def total_price(price, quantity):
return price * quantity
assert total_price(8, 3) == 24
assert total_price(8, 0) == 0
print("2 checks passed")
Expected output
2 checks passedCheck your understanding
Try each question before opening its explanation. These are free practice prompts; they do not use game chances.
Does a passing test prove that the whole application works?
No. It provides evidence for the scenarios and boundaries actually checked. Unchecked integrations, permissions or unusual inputs can still fail.
What makes a bug report reproducible?
Include the input, the steps, the expected result and the actual result, plus relevant environment details. Remove private information and keep the example as small as possible.
Which boundary case does the second check cover?
It covers zero quantity. The result should be zero, which is a useful separate case from an ordinary positive multiplication.
Sources and further reading
These original examples explain foundational concepts. Use the official references for complete, current details and version-specific behaviour.
About this guide: AI-assisted educational content with original examples and linked primary references. Found an issue? Tell us what to improve.