Search⌘ K
AI Features

Introduction

Explore why automated testing is crucial for Python object-oriented programs. Understand unit testing, test-driven development, and tools like unittest and pytest. Learn to write tests that ensure code reliability, maintainability, and correct behavior when evolving applications.

Skilled Python programmers agree that testing is one of the most important aspects of software development. Even though this chapter is placed near the end of the course, it is not an afterthought; everything we have studied so far will help us when writing tests. In this chapter, we’ll look at the following topics:

  • The importance of unit testing and test-driven development
  • The standard library unittest module
  • The pytest tool
  • The mock module
  • Code coverage

In the case study for this chapter, we’ll focus – no surprise – on writing some tests for the case study examples. We’ll start with some of the fundamental reasons why automated software testing is so important.

Why test?

Many programmers already know how important it is to test their code. Some people argue that testing is more important in Python code because of its dynamic nature; compiled languages such ...