Testing and Debugging
Outcome 8: Testing and Debugging · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx
Objectives
- Distinguish syntax errors, runtime errors, and logic errors by their symptoms.
- Read a Python traceback and identify the file, line, and error type.
- Apply print debugging to inspect variable values at key points.
- Write test cases including edge cases (empty, zero, maximum, invalid input).
- Write assert statements to automate correctness checks for functions.
Key terms
- syntax error
- Violation of Python grammar rules — the program cannot be parsed and will not run.
- runtime error
- An exception raised while the program runs: ZeroDivisionError, IndexError, TypeError, FileNotFoundError.
- logic error
- The program runs without exceptions but produces incorrect results due to a flaw in the algorithm.
- exception
- A runtime event that disrupts normal execution; Python raises a named exception object with a message.
- traceback
- The printed chain of function calls that led to an exception — read it bottom-up: the last line names the error.
- debugging
- Systematically finding and fixing errors in a program.
- print debugging
- Inserting print() statements to display variable values and confirm control flow at specific points.
- test case
- A set of inputs and the expected output used to verify that a function behaves correctly.
- edge case
- Extreme or boundary inputs that test the limits of a function: empty string, zero, negative, very large value.
- assert
- A statement that raises AssertionError if its condition is False; used to write inline test checks.
The concept
Errors are not failures — they are information. Every Python error message tells you exactly what went wrong and where. Learning to read and interpret these messages is as important as learning to write code.
Errors fall into three categories. A syntax error violates Python's grammar and prevents the program from running at all. Python reports the file name, line number, and a caret (^) pointing at the problem. Fix syntax errors first — nothing else can run until they are resolved. Examples: missing colon after if, mismatched parentheses, misspelled keyword.
A runtime error occurs while the program is executing. Python raises an exception with a name (TypeError, ZeroDivisionError, IndexError, NameError, FileNotFoundError) and a message. The traceback shows the chain of calls that led to the error — read it from the bottom up: the last frame is where the exception occurred; earlier frames show how you got there. Fix runtime errors by addressing the specific cause: guard against zero division (if denominator != 0:), handle missing files (try/except FileNotFoundError), convert types before comparison (int(user_input)).
A logic error is the most dangerous because Python does not detect it. The program runs, produces output, and exits cleanly — but the output is wrong. Examples: using + instead of *, forgetting to update an accumulator, off-by-one in a loop range. The only way to find logic errors is testing: run the program with known inputs and compare to expected outputs.
Print debugging is the fastest tool for diagnosing both runtime and logic errors. Insert print(f'x={x}, y={y}') before and after suspected lines. Confirm the values are what you expect at each step. Remove the print statements once the bug is fixed.
Test cases should include normal cases (typical inputs), boundary cases (minimum and maximum valid inputs), and edge cases (empty collections, zero, negative numbers, non-numeric strings if input is expected to be numeric). Write assert statements to automate checks: assert add(2, 3) == 5, 'add failed'. If the assertion fails, Python raises AssertionError with your message.
Blueprint Pillar 3 — Technology and Society: Bugs in software have caused spacecraft crashes, medical device failures, financial losses, and privacy breaches. The Ariane 5 rocket exploded in 1996 due to a data type overflow — a runtime error nobody tested for. Systematic testing and debugging are not optional extras — they are ethical requirements for software that affects real people.
Worked examples
Common mistakes
- Not reading the error message. Most new programmers close the error window and guess at the fix. The traceback tells you exactly where and what went wrong — read it every time.
- Only testing happy path (normal valid input). A function that works for 5 may fail for 0, -1, or an empty list. Test edge cases before claiming a function is correct.
- Leaving print debugging statements in final code. Once the bug is fixed, remove all debugging print statements — they clutter output and can expose sensitive variable values.
Self-check
Try each one before you look. A miss here costs nothing and tells you exactly what to reread.
Canvas is the official record. This companion enhances the PGCC curriculum; it does not replace it. Last name and class year only. Students with a 504 plan or IEP: your accommodations apply.