Giter Site home page Giter Site logo

Comments (9)

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
Fair enough. Unfortunately, the Testing on the Toilet won't be very helpful, 
since the examples rely on internal modules. What I can do is show how to use 
pyfakefs with unittest. Your production code remains untouched in your 
production modules, and the _test.py files with your unit test will mock the os 
module in your production module with pyfakefs. No changes are required to your 
production module.

The following is untested:

import unittest
import fake_filesystem
import yourmodule

class GroupOfTests(unittest.TestCase):
  def setUp(self):
    self.stubs = unittest.StubOutForTesting()

    # this fakes the os and os.path modules
    self.fs = fake_filesystem.FakeFilesystem()
    self.os = fake_filesystem.FakeOsModule(self.fs)
    self.stubs.SmartSet(yourmodule, 'os', self.os)

    # this redirects any open() for file() calls to the fake filesystem
    self.fake_open = fake_filesystem.FakeFileOpen(self.fs)
    self.stubs.SmartSet(__builtins__, 'file', self.fake_open)
    self.stubs.SmartSet(__builtins__, 'open', self.fake_open)

    # in tests, manipulate the fake filesystem through the
    # FakeFilesystem() object
    self.fs.CreateDirectory('/adir')
    self.fs.CreateFile('/adir/afile', contents='what ever')

  def tearDown(self):
    self.stubs.SmartUnsetAll()

  def testSomething(self):
    # test the fake filesystem through the FakeFilesystem() object
    self.assertTrue(self.fs.Exists('/adir/afile'))

Original comment by [email protected] on 1 Oct 2013 at 11:34

  • Changed state: Started

from pyfakefs.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
Ah.  unittest.StubOutForTesting().  After I get this going, I'll try to get 
back to write up a wiki article for you.

Thank you for your quick and complete response.

Original comment by [email protected] on 1 Oct 2013 at 11:45

from pyfakefs.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
Now I am looking at refining (and publishing) this approach.

I have a question that is a matter of style.  Your example depends on the mox 
module because it uses mox.stubout.StubOutForTesting.  Should the published, 
"canonical" solution use mox, or should I switch to some other module like 
stub? 

Original comment by [email protected] on 14 Jan 2014 at 7:45

from pyfakefs.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
mox.stubout will work. Basically, something needs to manage the stub, even if 
it is just part of the test:

def setUp(self):
  self._save_file = __builtins__.file
  self._save_open = __builtins__.open
  __builtins__.file = self.fake_open
  __builtins__.open = self.fake_open

def tearDown(self):
  __builtins__.file = self._save_file
  __builtins__.open = self._save_open

Original comment by [email protected] on 14 Jan 2014 at 8:35

from pyfakefs.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
Okay, I'll stick with mox for now.

Original comment by [email protected] on 14 Jan 2014 at 9:15

from pyfakefs.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
I have a new base class pyfakefs.fake_filesystem_unittest.TestCase.  When you 
replace unittest.TestCase with this, it causes the fake filesystem to be used 
by all the tests and the software under test.

I also created an example module and a test for it.  This demonstrates how to 
use pyfakefs.fake_filesystem_unittest.TestCase.

I would like to push this code for you to scrutinize, and I would like to write 
a "Getting Started" wiki article.

I do not see any "pull request" functionality on Google Code.  How can we 
proceed?

Original comment by [email protected] on 16 Jan 2014 at 2:26

Attachments:

from pyfakefs.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
I have a solution for doctest, too.  Are you interested?

Original comment by [email protected] on 23 Jan 2014 at 5:31

from pyfakefs.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 17, 2024
See https://code.google.com/r/jmcgeheeiv-pyfakefs/ for code that supports both 
unittest and doctest, plus a practical example and tutorial. 

Original comment by [email protected] on 9 May 2014 at 6:28

from pyfakefs.

jmcgeheeiv avatar jmcgeheeiv commented on May 17, 2024

Upon moving this project from Google Code to GitHub project jmcgeheeiv/pyfakefs, I merged the jmcgeheeiv-pyfakefs files, and edited the usage tutorial into the wiki.

from pyfakefs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.