Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.10.1

Testing and Debugging in Python

Learn to design test cases, use assertions, isolate defects, read tracebacks and refine code systematically.

01

Learning objectives

  • Distinguish syntax, runtime and logical defects.
  • Design normal, boundary and invalid-input test cases.
  • Use assertions to check program assumptions.
  • Follow a reproducible debugging process and interpret tracebacks.
02

Lesson notes

A program that runs without crashing may still be wrong. Testing compares actual behaviour with expected behaviour under selected inputs. A test case should state the input, the expected result and the condition being checked. A useful test set contains typical values, boundaries, invalid inputs and edge cases such as empty collections.

A unit test checks a small function independently. An integration test checks the interaction between components. System testing checks the complete product, while user acceptance testing checks whether the result meets stakeholder needs. A regression test repeats an earlier test after a change to make sure a previously fixed behaviour has not broken again.

Assertions express assumptions that should hold at a particular point in the program. For example, assert total >= 0 documents an invariant. Assertions are development checks, not a replacement for validating untrusted user input: production code must respond safely to invalid values rather than assuming they never occur.

Debugging is a controlled investigation. Reproduce the failure, read the final traceback line first, locate the relevant statement, inspect the values that reach it, form one hypothesis, change one thing and rerun the tests. Keep a failing test as a regression test. Avoid random edits because they can hide the original defect and introduce new ones.

03

Worked code examples

Worked example

The function validates its denominator, produces a predictable result and uses an assertion to check a known example.

def percentage(score, total):
    if total <= 0:
        raise ValueError("total must be positive")
    return score / total * 100

assert percentage(45, 50) == 90
print("Boundary check:", percentage(0, 50))
Output
Boundary check: 0.0
04

Key terminology

Test case

A defined input and conditions with an expected result.

Boundary test

A test focused on minimum, maximum or transition values.

Regression test

A test repeated after a change to guard against reintroducing a defect.

Assertion

A development-time statement that checks an assumption.

05

Common misconceptions

Common mistake

One successful example proves that a function is correct.

Correct understanding

A useful test set covers normal, boundary, invalid and edge cases.

Common mistake

Debugging means changing lines until the error disappears.

Correct understanding

Debugging should use reproducible evidence, a hypothesis and controlled changes.

06

Check your understanding

1. Which test checks a minimum or maximum allowed input?

2. What should be read first in a Python traceback?

3. A regression test is intended to:

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.