rubik
rubik

Reputation: 9104

Mock patches don't work more than once

I cannot get mock's patches to work. In the following code, only the first assertEqual inside test_base succeeds. If I swap the first with the second, again only the first one succeeds.

import os
import mock

def fake_isfile(filename):
    if filename == 'file.py':
        return True
    return False


def fake_walk():
    yield '.', ['tests', 'sub', '.hid'], ['tox.ini', 'amod.py', 'test_all.py']
    yield './tests', [], ['test_amod.py', 'run.py', '.hid.py']
    yield './sub', [], ['amod.py', 'bmod.py']


class TestIterFilenames(unittest.TestCase):

    def setUp(self):
        self.iter_files = lambda *a, **kw: list(tools.iter_filenames(*a, **kw))

    def test_stdin(self):
        self.assertEqual(self.iter_files(['-']), ['-'])

    @mock.patch('tools.os.path')
    @mock.patch('tools.os')
    def test_all(self, os_mod, os_path_mod):
        os_path_mod.normpath = os.path.normpath
        os_path_mod.basename = os.path.basename
        os_path_mod.join = os.path.join
        os_path_mod.isfile.side_effect = fake_isfile
        os_mod.walk.return_value = fake_walk()

        self.assertEqual(self.iter_files(['file.py', 'random/path']),
                         ['file.py', 'amod.py', 'test_all.py',
                          'tests/test_amod.py', 'tests/run.py', 'sub/amod.py',
                          'sub/bmod.py'])

        self.assertEqual(self.iter_files(['file.py', 'random/path'],
                                         'test_.*'),
                         ['file.py', 'amod.py', 'tests/test_amod.py',
                          'tests/run.py', 'sub/amod.py', 'sub/bmod.py'])

I looked into the documentation, and I found that to cleanup after every test method I had to do the following:

import os
import mock

def fake_isfile(filename):
    if filename == 'file.py':
        return True
    return False


def fake_walk():
    yield '.', ['tests', 'sub', '.hid'], ['tox.ini', 'amod.py', 'test_all.py']
    yield './tests', [], ['test_amod.py', 'run.py', '.hid.py']
    yield './sub', [], ['amod.py', 'bmod.py']


class TestIterFilenames(unittest.TestCase):

    def setUp(self):
        self.iter_files = lambda *a, **kw: list(tools.iter_filenames(*a, **kw))

        self.patcher1 = mock.patch('radon.cli.tools.os.path')
        self.patcher2 = mock.patch('radon.cli.tools.os')

        os_path_mod = self.patcher1.start()
        os_mod = self.patcher2.start()
        os_path_mod.normpath = os.path.normpath
        os_path_mod.basename = os.path.basename
        os_path_mod.join = os.path.join
        os_path_mod.isfile.side_effect = fake_isfile
        os_mod.walk.return_value = fake_walk()

    def tearDown(self):
        self.patcher1.stop()
        self.patcher2.stop()

    def test_base(self):
        self.assertEqual(self.iter_files(['file.py', 'random/path']),
                         ['file.py', 'amod.py', 'test_all.py',
                          'tests/test_amod.py', 'tests/run.py', 'sub/amod.py',
                          'sub/bmod.py'])

    def test_exclude(self):
        self.assertEqual(self.iter_files(['file.py', 'random/path'],
                                         'test_.*'),
                         ['file.py', 'amod.py', 'tests/test_amod.py',
                          'tests/run.py', 'sub/amod.py', 'sub/bmod.py'])

However, even in this case the second test method fails because fake_walk does not get called (I am sure of this).

Upvotes: 0

Views: 193

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121316

Your fake_walk is a generator, and as such it can only be iterated over once. Use it as a side_effect instead so it is called anew each time os.walk() is called:

os_mod.walk.side_effect = fake_walk

Since you are not actually testing if os.walk() is called, you may as well just set the whole attribute to the function:

os_mod.walk = fake_walk

Upvotes: 2

Related Questions