O'Reilly logo

Modern Python Cookbook by Steven F. Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

In the Mocking External Resources recipe in Chapter 9, Testing, we looked at ways to mock external resources so that we could write a unit test that wouldn't accidentally delete files. A test for the code in this recipe needs to mock the filesystem resources by mocking the Path class. Here's how the unit test would look, starting with a high-level outline of the test class:

    import unittest 
    from unittest.mock import * 
 
    class GIVEN_get_config_WHEN_load_THEN_overrides(unittest.TestCase): 
        def setUp(self): 
 
        def runTest(self): 

This provides a boilerplate structure for a unit test. Mocking a Path becomes rather complex because of the number of distinct objects involved. Here's a summary of what kinds of object creations occur:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required