Exceptions & Testing

5. Exceptions & Testing#

Errors are inevitable in any program. This chapter teaches you to recover from runtime failures and find logic errors before they reach users.

  • Exception handling for runtime failures

  • Tracebacks as debugging evidence

  • Custom exceptions for clearer error reporting

  • Debugging techniques and logging to find and record problems

  • Tests as executable checks of program behavior

Chapter Video

This overview introduces exception handling with try, except, else, and finally.

Python Tutorial: Using Try/Except Blocks for Error Handling (10:34) by Corey Schafer

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Distinguish between syntax errors, runtime errors, and semantic errors

  2. Read and interpret Python tracebacks to locate the source of a bug

  3. Handle exceptions gracefully with try, except, else, and finally

  4. Raise exceptions and define custom exception classes

  5. Track down bugs with print-statement debugging, assertions, and bisection, and record diagnostics with logging

  6. Write tests with pytest, unittest, and doctest, including tests that expect an exception

  7. Isolate code from dependencies with mocks, test many inputs with parametrization, and read a coverage report

Chapter Flow

Glossary

#

Term

Explanation

1

Exception

An object that represents an error or unusual condition during program execution.

2

Exception handling

Code that responds to exceptions so a program can recover or fail clearly.

3

try block

The block of code where Python watches for exceptions.

4

except block

The block that runs when a matching exception occurs.

5

else block

The block that runs after a try block if no exception occurs.

6

finally block

The block that runs whether or not an exception occurs.

7

Traceback

A report showing the call path and location of an exception.

8

Debugging

The process of finding and fixing problems in a program.

9

Runtime error

An error that occurs while a program is running.

10

Semantic error

A logic error where code runs but produces the wrong result.

11

Raise

To deliberately signal an exception with the raise statement.

12

Custom exception

An exception class defined for a program-specific error case.

13

Logging

Recording events, warnings, and errors from a running program.

14

Log level

A severity category for a log message, such as DEBUG, INFO, WARNING, ERROR, or CRITICAL.

15

Unit test

A test that checks a small part of a program in isolation.

16

Assertion

A check, written with assert, that a condition is true; you use it in tests and as an in-code developer check of your program’s assumptions.

17

Doctest

A test written in a docstring using interactive Python examples.

18

Fixture

Setup data or resources used by tests.

19

Mock

A replacement object used to isolate code from an external dependency.

20

Test coverage

A measure of which lines or branches of code were exercised by tests.

21

Syntax error

An error in code that violates Python’s grammar, reported before any of the program runs.

22

Bisection

A debugging technique that checks the program’s state at a midpoint to find which half contains the bug, then repeats on that half.

23

Parametrized test

A single test function that runs once for each set of inputs, written with @pytest.mark.parametrize.

24

Dependency injection

A design in which a function receives the objects it depends on as arguments, so a test can pass in a stand-in.

25

Fake

A simple stand-in object that returns canned data in place of a real dependency during a test.

26

Integration test

A test that checks several units of a program working together.

27

Test-driven development

A workflow in which you write the tests first, then write the code that makes them pass.

28

Test discovery

The way pytest finds tests automatically by collecting test_*.py files and functions whose names start with test_.

Chapter Overview Slides