Python: Creating a mock or fake directory with files for unittesting
<p>I am trying to create a unit test for the following function:</p>

<pre><code>def my_function(path):
    #Search files at the given path
    for file in os.listdir(path):
        if file.endswith(".json"):
            #Search for file i'm looking for
            if file == "file_im_looking_for.json":
                #Open file
                os.chdir(path)
                json_file=json.load(open(file))
                print json_file["name"]
</code></pre>

<p>However I am having trouble successfully creating a fake directory with files in order for the function to work correctly and not through errors.</p>

<p>Below is what I have so far but it is not working for me, and I'm not sure how to incorporate "file_im_looking_for" as the file in the fake directory.</p>

<pre><code>tmpfilepath = os.path.join(tempfile.gettempdir(), "tmp-testfile")
@mock.patch('my_module.os')

def test_my_function(self):

    # make the file 'exist'
    mock_path.endswith.return_value = True

    file_im_looking_for=[{
      "name": "test_json_file",
      "type": "General"
    }]

    my_module.my_function("tmpfilepath")
</code></pre>

<p>Any advice where I'm going wrong or other ideas to approach this problem are appreciated!</p>