Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock doesn't work after setUpPyfakefs #334

Closed
CausticYarn opened this issue Dec 12, 2017 · 5 comments
Closed

Mock doesn't work after setUpPyfakefs #334

CausticYarn opened this issue Dec 12, 2017 · 5 comments
Labels

Comments

@CausticYarn
Copy link

CausticYarn commented Dec 12, 2017

After I run a fake_filesystem_unittest.TestCase with the self.setUpPyfakefs() method, unittest.mock doesn't seem to work correctly in subsequent test cases.

Here is an example that reproduces the issue:

import os
import unittest.mock

from pyfakefs import fake_filesystem_unittest


class TestFakeFsUnittest(fake_filesystem_unittest.TestCase):
    def setUp(self):
        print('{} Before setUpPyfakefs: {}'.format(self.__class__.__name__,
                                                   os.path))
        self.setUpPyfakefs()
        print('{} After setUpPyfakefs: {}'.format(self.__class__.__name__,
                                                  os.path))

    def test_none(self):
        pass


class TestMockOsPath(unittest.TestCase):
    def test_rename(self):
        print('{} before mock: {}'.format(self.__class__.__name__,
                                          os.path))
        with unittest.mock.patch('os.path'):
            print('{} after mock: {}'.format(self.__class__.__name__,
                                             os.path))

When the TestFakeFsUnittest runs first, I get:

TestFakeFsUnittest Before setUpPyfakefs: <module 'ntpath' from 'C:\\pyenv\\dev36\\lib\\ntpath.py'>
TestFakeFsUnittest After setUpPyfakefs: <pyfakefs.fake_filesystem.FakePathModule object at 0x000001F8E59645C0>
TestMockOsPath before mock: <module 'ntpath' from 'C:\\pyenv\\dev36\\lib\\ntpath.py'>
TestMockOsPath after mock: <module 'ntpath' from 'C:\\pyenv\\dev36\\lib\\ntpath.py'>

But if TestMockOsPath runs first (e.g. by renaming TestFakeFsUnittest to be alphabetically after) I get:

TestMockOsPath before mock: <module 'ntpath' from 'C:\\pyenv\\dev36\\lib\\ntpath.py'>
TestMockOsPath after mock: <MagicMock name='path' id='2650908079776'>
TestZFakeFsUnittest Before setUpPyfakefs: <module 'ntpath' from 'C:\\pyenv\\dev36\\lib\\ntpath.py'>
TestZFakeFsUnittest After setUpPyfakefs: <pyfakefs.fake_filesystem.FakePathModule object at 0x0000026936764E10>
@mrbean-bremen
Copy link
Member

That's interesting - I could reproduce it. I didn't understand it yet (seems to be some module caching issue), and the obvious workaround is to put such tests into separate files, but I will have another look later.

@CausticYarn
Copy link
Author

CausticYarn commented Dec 12, 2017

Having the test in separate files doesn't help (at least when using a test suite like Nose to run all tests). I created the above example as the simplest reproduction, but I originally discovered the issue from changing one of my tests to use the setUpPyfakefs call, and having tests in another module fail.

@mrbean-bremen
Copy link
Member

Hm, ok, you are right - this won't help if this a module caching issue. One (ugly) way to avoid this is to import os inside the test - but there has to be a better way. I will probably have another look tomorrow.

@mrbean-bremen
Copy link
Member

Fixed by @absolutelyNoWarranty in #336.

@mrbean-bremen
Copy link
Member

Now fixed the root cause (missing cleanup) in #340.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants