Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Coding Exercises: Hello

1. Functions and Testing

In this chapter your goal is to become familiar with functions, and writing unit tests to test your functions. This is the most important chapter of the book.

Unit tests are low-level tests of a single function (the target function). In the unit test function, you are given sample input and the expected output (return value). The unit test function runs the target function with the given input and checks that the return value matches what was expected.

Unit test functions are important for verifying the behavior of your code. As you add more code to your project, it is easy to introduce new bugs. Running the unit tests after any changes will give you confidence that your functions still behave as you expect.

Unit tests are also important for ongoing maintenance of your code. For example, if you find a new bug, you can:

In the software development world, this procedure of writing tests first is called test driven development (TDD).


Next: