mardi 28 juin 2016

python: how to mock the input dir for my unittest?

Related to a earlier question of mine. I wanna unittest this function

def get_dir_size(dir_path):
    """Determine the size of a dir.

    This function also takes into account the allocated size of
    directories (4096 bytes).

    Note: The function may crash on symlinks with something like:
    OSError: [Errno 40] Too many levels of symbolic links

    :param dir_path (str): path to the directory
    :return: size in bytes.
    """
    tot_size = 0
    for (root_path, dirnames, filenames) in os.walk(dir_path):
        for f in filenames:
            fpath = os.path.join(root_path, f)
            tot_size += os.path.getsize(fpath)
        tot_size += os.path.getsize(root_path)
    return tot_size

So from what I understood I have to mock the os.walk function

import threedi_utils

@mock.patch('threedi_utils.files.os.walk')
def test_get_dir_size_can_get_dir_size(self, mock_walk):
    mock_walk.return_value(5000)
    size = threedi_utils.files.get_dir_size(self.test_path)
    self.assertEqual(size, 5000)

But mock_walk.return_value(5000) goes without effect as my test fails

Traceback (most recent call last):
  File "/home/vagrant/.buildout/eggs/http://ift.tt/1LRgPTF", line 1305, in patched
    return func(*args, **keywargs)
  File "/srv/lib/threedi_utils/tests/test_files.py", line 55, in test_get_dir_size_can_get_dir_size
    self.assertEqual(size, 5000)
AssertionError: 0 != 5000

What am I missing?

Aucun commentaire:

Enregistrer un commentaire