...

/

The unittest Module

The unittest Module

Learn how to use the unittest module in Python to perform unit testing.

The unittest module is a great option with which to start writing unit tests because it provides a rich API to write all kinds of testing conditions, and since it's available in the standard library, it's quite versatile and convenient.

The unittest module is based on the concepts of JUnit (from Java), which, in turn, is also based on the original ideas of unit testing that come from Smalltalk (perhaps this is the reason behind the naming convention of the methods on this module), so it’s object-oriented in nature. For this reason, tests are written through classes, where the checks are verified by methods, and it’s common to group tests by scenarios in classes.

Press + to interact

Creating unit tests

To start writing unit tests, we have to create a test class that inherits from unittest.TestCase, and define the conditions we want to stress on its methods. These methods should start with test_*, and can internally use any of the methods inherited from unittest.TestCase to check conditions that ...