Summarize the concept explored in this chapter regarding testing object-oriented programs in Python.

We'll cover the following


In this chapter, we’ve looked at a number of topics related to testing applications written in Python. These topics include the following:

  • We described the importance of unit testing and test-driven development as a way to be sure our software does what is expected.

  • We started by using the unittest module because it’s part of the standard library and readily available. It seems a little wordy, but otherwise works well for confirming that our software works.

  • The pytest tool requires a separate installation, but it seems to produce tests that are slightly simpler than those written with the unittest module. More importantly, the sophistication of the fixture concept lets us create tests for a wide variety of scenarios.

  • The mock module, part of the unittest package, lets us create mock objects to better isolate the unit of code being tested. By isolating each piece of code, we can narrow our focus on being sure it works and has the right interface. This makes it easier to combine components.

  • Code coverage is a helpful metric to ensure that our testing is adequate. Simply adhering to a numeric goal is no substitute for thinking, but it can help to confirm that efforts were made to be thorough and careful when creating test scenarios.

We’ve been looking at several kinds of tests with a variety of tools:

  • Unit tests with the unittest package or the pytest package, often using Mock objects to isolate the fixture or unit being tested.

  • Integration tests, also with unittest and pytest, where more complete integrated collections of components are tested.

  • Static analysis can use mypy to examine the data types to be sure they’re used properly. This is a kind of test to ensure the software is acceptable. There are other kinds of static tests, and tools like flake8, pylint, and pyflakes can be used for these additional analyses.

Some research will turn up scores of additional types of tests. Each distinct type of test has a distinct objective or approach to confirming the software works. A performance test, for example, seeks to establish the software is fast enough and uses an acceptable number of resources. We can’t emphasize enough how important testing is. Without automated tests, software can’t be considered complete or even usable. Starting from test cases lets us define the expected behavior in a way that’s specific, measurable, achievable, results-based, and trackable: SMART.


We have finally covered the most important topic in Python programming: automated testing. Test-driven development is considered a best practice. The standard library unittest module provides a great out-of-the-box solution for testing, while the pytest framework has some more Pythonic syntaxes. Mocks can be used to emulate complex classes in our tests. Code coverage gives us an estimate of how much of our code is being run by our tests, but it does not tell us that we have tested the right things.

Get hands-on with 1200+ tech skills courses.