From 3bdca2d2c1ebb272db3421f90e03ea8426a239f6 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Thu, 20 Sep 2018 21:47:19 +0200 Subject: [PATCH] Added TestCase arguments to setUpPyfakefs() for convenience --- CHANGES.md | 1 + docs/usage.rst | 3 +++ pyfakefs/fake_filesystem_unittest.py | 26 +++++++++++++++---- .../tests/fake_filesystem_unittest_test.py | 25 +++++++++++------- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1b46e96c..744b15fa 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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)) diff --git a/docs/usage.rst b/docs/usage.rst index dd6777c9..6bbb4b13 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -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, diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index aaaf841a..19c153d6 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -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) diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 212b3c91..e3786364 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -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' @@ -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))