Skip to content

Commit

Permalink
Added TestCase arguments to setUpPyfakefs() for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Oct 1, 2018
1 parent 8209b61 commit 3bdca2d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The release versions are PyPi releases.
#### New Features
* parameter `patch_path` has been removed from `UnitTest` and `Patcher`,
the correct patching of `path` imports is now done automatically
* `UnitTest` /`Patcher` arguments can now also be set in `setUpPyfakefs()`
* added pathlib2 support ([#408](../../issues/408)) ([#422](../../issues/422))
* added some support for extended filesystem attributes under Linux
([#423](../../issues/423))
Expand Down
3 changes: 3 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ Customizing Patcher and TestCase
Both ``fake_filesystem_unittest.Patcher`` and ``fake_filesystem_unittest.TestCase``
provide a few additional arguments for fine-tuning. These are only needed if
patching does not work for some module.
In case of ``fake_filesystem_unittest.TestCase``, these arguments can either
be set in the TestCase instance initialization, or in the call of
``setUpPyfakefs()``.

.. note:: If you need these arguments in ``PyTest``, you have to
use ``Patcher`` directly instead of the ``fs`` fixture. Alternatively,
Expand Down
26 changes: 21 additions & 5 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,35 @@ def __init__(self, methodName='runTest'):
def fs(self):
return self._stubber.fs

def setUpPyfakefs(self):
def setUpPyfakefs(self,
additional_skip_names=None,
use_dynamic_patch=None,
modules_to_reload=None,
modules_to_patch=None):
"""Bind the file-related modules to the :py:class:`pyfakefs` fake file
system instead of the real file system. Also bind the fake `open()`
function, and on Python 2, the `file()` function.
Invoke this at the beginning of the `setUp()` method in your unit test
class.
For the arguments, see the `TestCaseMixin` attribute description.
If any of the arguments is not None, it overwrites the settings for
the current test case. Settings the arguments here may be a more
convenient way to adapt the setting than overwriting `__init__()`.
"""
if additional_skip_names is None:
additional_skip_names = self.additional_skip_names
if use_dynamic_patch is None:
use_dynamic_patch = self.use_dynamic_patch
if modules_to_reload is None:
modules_to_reload = self.modules_to_reload
if modules_to_patch is None:
modules_to_patch = self.modules_to_patch
self._stubber = Patcher(
additional_skip_names=self.additional_skip_names,
use_dynamic_patch=self.use_dynamic_patch,
modules_to_reload=self.modules_to_reload,
modules_to_patch=self.modules_to_patch)
additional_skip_names=additional_skip_names,
use_dynamic_patch=use_dynamic_patch,
modules_to_reload=modules_to_reload,
modules_to_patch=modules_to_patch)

self._stubber.setUp()
self.addCleanup(self._stubber.tearDown)
Expand Down
25 changes: 15 additions & 10 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,23 @@ def test_fakepathlib(self):
self.assertTrue(self.fs.exists('/fake_file.txt'))


class TestImportAsOtherName(fake_filesystem_unittest.TestCase):
def __init__(self, methodName='RunTest'):
modules_to_load = [pyfakefs.tests.import_as_example]
super(TestImportAsOtherName, self).__init__(
methodName, modules_to_reload=modules_to_load)
class TestImportAsOtherNameInit(fake_filesystem_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs(
modules_to_reload=[pyfakefs.tests.import_as_example])

def test_file_exists(self):
file_path = '/foo/bar/baz'
self.fs.create_file(file_path)
self.assertTrue(self.fs.exists(file_path))
self.assertTrue(
pyfakefs.tests.import_as_example.check_if_exists(file_path))


class TestImportAsOtherNameSetup(fake_filesystem_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs()
self.setUpPyfakefs(
modules_to_reload=[pyfakefs.tests.import_as_example])

def test_file_exists(self):
file_path = '/foo/bar/baz'
Expand Down Expand Up @@ -180,10 +189,6 @@ class TestPathNotPatchedIfNotOsPath(TestPyfakefsUnittestBase):
An own path module (in this case an alias to math) can be imported
and used.
"""

def __init__(self, methodName='runTest'):
super(TestPathNotPatchedIfNotOsPath, self).__init__(methodName)

def test_own_path_module(self):
self.assertEqual(2, path.floor(2.5))

Expand Down

0 comments on commit 3bdca2d

Please sign in to comment.