How to do it...

  1. For this example, we'll assume that the unittest test cases are the same module as the code being tested. Import the unittest and unittest.mock modules:
        import unittest 
        from unittest.mock import * 

The unittest module is simply imported. To use the features of this module, we'll have to qualify the names with unittest.. The various names from unittest.mock were all imported so the names can be used without any qualifier. We'll use a number of features of the mock module, and the long qualifying name is awkward.

  1. Include the code to be tested. This is shown previously.
  2. Create the following skeleton for testing. We've provided one class definition, plus a main script that can be used to execute the tests:
 class GIVEN_data_WHEN_save_data_THEN_file(unittest.TestCase): ...

Get Modern Python Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.