是否可以在 Python 3.6 中模拟内置的 len()
函数?
我有一个类,它定义了一个依赖于 len()
函数的简单方法,如下所示:
class MyLenFunc(object):
def is_longer_than_three_characters(self, some_string):
return len(some_string) > 3
我正在尝试为此方法编写单元测试,但我无法在不产生错误的情况下模拟 len()
函数。
这是我到目前为止所拥有的:
import unittest
from unittest.mock import patch
import my_len_func
class TestMyLenFunc(unittest.TestCase):
@patch('builtins.len')
def test_len_function_is_called(self, mock_len):
# Arrange
test_string = 'four'
# Act
test_object = my_len_func.MyLenFunc()
test_object.is_longer_than_three_characters(test_string)
# Assert
self.assertEqual(1, mock_len.call_count)
if __name__ == '__main__':
unittest.main()
我找到了另一个 SO 问题/答案 here这表明不可能模拟内置函数,因为它们是不可变的。但是,我又找到了几个网站 here和 here这表明否则。我对上面的单元测试类的尝试直接取自这些网站中的后者(是的,我已经尝试了那里提到的其他技术。都以同样的错误结束)。
我遇到的错误太长,无法发布完整内容,因此我将删除其中重复的部分(您会看到它是从错误消息的最后部分递归而来的)。错误文本如下:
ERROR: test_len_function_is_called (__main__.TestMyLenFunc)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python36\Lib\unittest\mock.py", line 1179, in patched
return func(*args, **keywargs)
File "C:/Python/scratchpad/my_len_func_tests.py", line 16, in test_len_function_is_called
test_object.is_longer_than_three_characters(test_string)
File "C:\Python\scratchpad\my_len_func.py", line 3, in is_longer_than_three_characters
return len(some_string) > 3
File "C:\Python36\Lib\unittest\mock.py", line 939, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "C:\Python36\Lib\unittest\mock.py", line 949, in _mock_call
_call = _Call((args, kwargs), two=True)
File "C:\Python36\Lib\unittest\mock.py", line 1972, in __new__
_len = len(value)
...
(Repeat errors from lines 939, 949, and 1972 another 95 times!)
...
File "C:\Python36\Lib\unittest\mock.py", line 939, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "C:\Python36\Lib\unittest\mock.py", line 944, in _mock_call
self.called = True RecursionError: maximum recursion depth exceeded while calling a Python object
----------------------------------------------------------------------
Ran 1 test in 0.004s
FAILED (errors=1)
非常感谢。
最佳答案
不要修补 builtins.len
; mock
library 中的 代码现在中断了,因为它需要函数才能正常运行!修补被测模块的全局变量:
@patch('my_len_func.len')
这添加全局len
到你的模块,len(some_string)
在MyLenFunc().is_longer_than_three_characters( )
方法将找到一个而不是内置函数。
但是,我必须说,测试是否调用了 len()
感觉是个错误的测试。您想要检查该方法是否针对给定的输入产生了正确的结果,len()
等基本操作只是实现该目标的小构建 block ,通常不会在该范围内进行测试。
关于python - 是否可以模拟 Python 3.6 中的内置 len() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44920344/