Writing unit tests in Python

A tutorial in bullet points

David Piper
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:
Simple example of a test class.
  • if test succeeded, this is the output:
All test succeeded and that pretty fast!
  • for executing all tests at once, place all test cases in one directory and add following file:
Easier test execution by running all tests at once.
  • by executing this file from the command line, all test cases are executed
  • always remember: Test fast, test often!

--

--

David Piper
David Piper

Written by David Piper

Working as an iOS and Android dev.

No responses yet