← Python Programming
Object-oriented Python and projects · 15 MIN

Testing and debugging

Testing and debugging

Assertions are useful for simple internal checks. The unittest module supports repeatable automated tests. Test normal cases, boundary cases and invalid input. Debug by reducing the problem, inspecting values and reading tracebacks from the bottom upward.

Try it yourself

Change the example, run it, and explain the result in your own words. Then create one variation of your own.

Key idea

Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.

Code example

def add(a, b):
    return a + b

assert add(2, 3) == 5
assert add(-1, 1) == 0