Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

lutzky/fakefile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fakefile Build Status

Sometimes your code needs to write some files, and you want to test that it does this correctly. This library provides a relatively simple way to do it.

import fakefile
import unittest
import mock

def my_function():
    with open("somefile", "w") as f:
        f.write("correct output")
    with open("existing_file", "w") as f:
        return f.read()
  

class TestMyCode(unittest.TestCase):
    def test_my_function(self):
        faker = fakefile.FakeFile()
    
        faker.set_contents("existing_file", "correct input")
    
        with mock.patch('__builtin__.open', faker.open):
            result = my_function()  # No file "somefile" will be created!
                                    # No file "existing_file" will be read!
        self.assertEquals(faker.files["somefile"].file_contents,
                          "correct output")

After writing, it turns out that Google has a much more advanced version of this, known as pyfakefs.

About

A simple fake file implementation for Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages