Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为我的单元测试模拟输入目录?_Python_Python 2.7_Unit Testing_Mocking - Fatal编程技术网

Python 如何为我的单元测试模拟输入目录?

Python 如何为我的单元测试模拟输入目录?,python,python-2.7,unit-testing,mocking,Python,Python 2.7,Unit Testing,Mocking,与我先前的一个问题有关: 我想对该函数进行单元测试: 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 lik

与我先前的一个问题有关: 我想对该函数进行单元测试:

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
因此,根据我的理解,我必须模拟
os.walk
函数

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)
但是
mock\u walk.return\u值(5000)
在测试失败时无效

Traceback (most recent call last):
  File "/home/vagrant/.buildout/eggs/mock-1.3.0-py2.7.egg/mock/mock.py", 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

我错过了什么?

好吧,我走错了路。应该模拟了
os.path.getsize()
方法。另外,返回值需要这样提供
。返回值=5000

@mock.patch('threedi_utils.files.os.path.getsize')
def test_get_dir_size_can_get_dir_size(self, mock_size):
    mock_size.return_value = 50
    size = threedi_utils.files.get_dir_size(self.test_path)
    self.assertEqual(size, 750)
    self.tearDown()

这完全没有道理。如果你模拟了整个
get\u dir\u size
,你到底在测试什么?!您在这里测试的是,您的mock返回您刚刚告诉它的值。如果你想模拟出
os.walk
,你需要模拟返回该函数实际会返回的内容,这样你就可以看到你的函数是否正确使用它。哈哈,我明白了!你想把你的评论转换成一个能引导我正确方向的答案吗?相反,我建议你(或者只是运行你正在模拟的函数)写一个
返回值
,它实际上代表了你期望得到的结果。这正是嘲笑的基础。文档中也有很多例子。我只是想给你一些信用。