Writing unit tests in Python
A tutorial in bullet points
1 min readAug 3, 2019
- start with importing unittests
- create new test class that inherit from unittest.TestCase
- write test method with logic and call assert methods on self, e.g. assertTrue, assertFalse or assertEqual
- you can add an extended error message as msg parameter of any assertion to describe the error
- calling unittest.main() in main method
- this is an example of a test class:
- if test succeeded, this is the output:
- for executing all tests at once, place all test cases in one directory and add following file:
- by executing this file from the command line, all test cases are executed
- always remember: Test fast, test often!