Comments (9)
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.
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.
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.
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.
Okay, I'll stick with mox for now.
Original comment by [email protected]
on 14 Jan 2014 at 9:15
from pyfakefs.
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.
I have a solution for doctest, too. Are you interested?
Original comment by [email protected]
on 23 Jan 2014 at 5:31
from pyfakefs.
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.
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)
- sdist is missing `tox.ini` HOT 3
- pyfakefs 5.3.4 + moto 4.2.13, test failed HOT 1
- Example usage docs using pathlib leads to errors HOT 2
- pyarrow.lib.ArrowKeyError: A type extension with name pandas.period already defined HOT 9
- Infinite loop with multiprocessing HOT 4
- EncodingWarnings raised (3) when using pyfakefs HOT 7
- Adding/removing directory entries does not respect directory ownership HOT 1
- Directory enumeration/search permissions mismatch Unix behavior HOT 3
- Wrong temporary directory created following a reset via `fs.os` HOT 4
- The `io.open` function should not consider file permissions when opening from an existing file descriptor HOT 3
- Patched `os.symlink` mishandles directory descriptors HOT 3
- Missing mock of `os.dup` HOT 4
- `os.getxattr` should raise `OSError(ENODATA)` for non-existent extended attributes HOT 1
- Patched `os.open` does not respect `O_NOFOLLOW` HOT 1
- Faked `os.link` is missing the `follow_symlinks` parameter HOT 1
- Fake `os.open` does not handle the `O_DIRECTORY` flag
- shutil.rmtree failing in OSType.WINDOWS with PermissionError HOT 2
- `os.open(O_DIRECTORY)` should raise `ENOENT` when the file does not exist
- Faked `os.makedirs` differs from the built-in `os.makedirs` and `mkdir -p` HOT 5
- Closing a duplicated fileno invalidates the previous file description
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyfakefs.