Using the unittest package
In this section, we're going to look at Python's standard unittest
package. We'll talk about how to structure a test file and how to write tests and draw a comparison between what happens and what should happen in these tests. Let's jump straight into it!
Structuring a test file
The unittest module contains a framework for performing automated unit testing. Most of this functionality is based around the unittest.TestCase
class, which we will inherit from to create our own tests.
In the following example, we see the basic features of TestCase
in action and also test and assert methods:

Any method that we define using a class inherited from TestCase
, and that has a name that starts with the word test, is assumed to be a unit test. In the preceding example, this means that the test_addition
method is a unit test; however, if we had added another method to the class, called connect
, the unittest module would not have treated it as a unit test.
Note
A TestCase
class can contain...