我有一种方法可以递归地迭代文件夹并生成其中的文件及其相应大小的字典。
`
def get_contents(folder_name) -> dict:
for item in os.listdir(folder_name):
item_path = os.path.join(folder_name, item)
if os.path.isdir(item_path):
get_contents(item_path)
else:
file_name = str(item_path)
<some code>
return some_dictionary`
我正在尝试使用 os.listdir()、os.path.join() 和 os.isdir() 的模拟为 if 语句编写单元测试。
这些是我为补丁指定的返回值。
`
def test_get_contents_dir_pass(self):
with patch('mymodule.os.listdir') as list_mock:
with patch('mymodule.os.path.isdir') as checkdir_mock:
with patch('mymodule.os.path.join') as path_mock:
with patch('builtins.open', new_callable = mock_open()) as mock_file:
list_mock.return_value = ['somedir']
path_mock.return_value = 'testapp/somedir'
checkdir_mock.return_value = True
actual = get_checksum('testapp')
assert <some assertion>`
我收到以下错误(我能够通过添加一些调试代码进行测试,if 语句/检查肯定已执行/达到)。 E RecursionError:超过最大递归深度 !!!检测到递归(相同的本地人和位置)
我对 else 语句的单元测试工作正常。
我认为这是我的模拟及其返回值的问题,但我不确定是什么。欢迎任何见解/提示。
我尝试了 list_mock 和 path_mock 的返回值的不同排列,但无济于事。