Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking Directory Structure in Python

I have some code below that I'm using to take an input of files, open and process, and then output some data. I've gotten the functionality working and I'm unit testing it now, below is an example of the code.

def foo(dir):
    path_to_search = join(dir, "/baz/foo")

   if isdir(path_to_search): 
   #path exists so do stuff...

        for fname in listdir(path_to_search):
            do_stuff()

   else:
       print "path doesn't exist"

I've been able to create a test where the past doesn't exist easily enough, but as you can see above I assert that the "/baz/foo" portion of the directory structure exists (in production the directory structure must have this file, in some cases it won't and we won't need to process it.)

I've tried to create a temporary directory structure using TempDir and join, but the code always kicks out saying the path doesn't exists.

Is it possible to mock the output of os.listdir such that I won't need to create a temporary directory structure that follows the needed /baz/foo convention?

like image 805
openingceremony Avatar asked Oct 17 '14 20:10

openingceremony


People also ask

What is mocking in Python?

mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used. unittest. mock provides a core Mock class removing the need to create a host of stubs throughout your test suite.

What is mocking and patching in Python?

mock provides a powerful mechanism for mocking objects, called patch() , which looks up an object in a given module and replaces that object with a Mock . Usually, you use patch() as a decorator or a context manager to provide a scope in which you will mock the target object.

What is mocking in Pytest?

In pytest , mocking can replace the return value of a function within a function. This is useful for testing the desired function and replacing the return value of a nested function within that desired function we are testing.

What is mocking in coding?

What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

What is mocking in Python and how to do it?

In Python, mocking is accomplished through the unittest.mock module. The module contains a number of useful classes and functions, the most important of which are the patch function (as decorator and context manager) and the MagicMock class. Mocking in Python is largely accomplished through the use of these two powerful components.

How does Python mock handle dumps?

For example, if you are mocking the json library and your program calls dumps (), then your Python mock object must also contain dumps (). Next, you’ll see how Mock deals with this challenge. A Mock must simulate any object that it replaces. To achieve such flexibility, it creates its attributes when you access them:

What is a directory in Python?

A directory or folder is a collection of files and sub directories. Python has the os module, which provides us with many useful methods to work with directories (and files as well).

How do Python mock objects work with JSON libraries?

Using an example from earlier, if you’re mocking the json library and you call dumps (), the Python mock object will create the method so that its interface can match the library’s interface: Notice two key characteristics of this mocked version of dumps ():


1 Answers

You don't need to create a fake directory structure, all you need to do is mock the isdir() and listdir() functions.

Using the unittest.mock library (or the external mock library, which is the exact same thing for Python versions < 3.3):

try:
    # Python >= 3.3 
    from unittest import mock
except ImportError:
    # Python < 3.3
    import mock

with mock.patch('yourmodule.isdir') as mocked_isdir, \
        mock.patch('yourmodule.listdir') as mocked_listdir:
    mocked_isdir.return_value = True
    mocked_listdir.return_value = ['filename1', 'filename2']

    yourmodule.foo('/spam/eggs')

    mocked_isdir.assert_called_with('/spam/eggs/baz/foo')
    mocked_listdir.assert_called_with('/spam/eggs/baz/foo')
like image 158
Martijn Pieters Avatar answered Sep 19 '22 14:09

Martijn Pieters