Unit Testing in Python Using unittest Module
The goal of this article is to provide a quick introduction to Python’s unit testing module called unittest. It is the essence, the very basic information you need to quickly start unit testing in Python.
Introduction to the unittest Module
Key points about unit testing in Python:
- modules with tests should import
unittest
module, - tests should be defined inside a class extending the
unittest.TestCase
class, - every test should start with the
test
word, - if a test has a doc. comment (between a pair of
'''
, i. e. three apostrophes), the comment will be printed when the test is being run (if verbose mode was set), - tests can have
setUp
andtearDown
methods – those methods will be called, respectively, before and after each of the tests; there are also class-level set up and tear down methods, - to execute the tests when the module is run,
unittest.main()
should be called, - to see which tests are called with additional info, the
-v
(verbose) parameter should be specified when the module with tests is executed.