python - 是否可以模拟 Python 3.6 中的内置 len() 函数?

标签 python python-3.x unit-testing mocking

是否可以在 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这表明不可能模拟内置函数,因为它们是不可变的。但是,我又找到了几个网站 herehere这表明否则。我对上面的单元测试类的尝试直接取自这些网站中的后者(是的,我已经尝试了那里提到的其他技术。都以同样的错误结束)。

我遇到的错误太长,无法发布完整内容,因此我将删除其中重复的部分(您会看到它是从错误消息的最后部分递归而来的)。错误文本如下:

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.lenmock 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/

相关文章:

Python 2.7 导入错误 : no module named _ssl

python - 使用 matplotlib 和 numpy 绘制方程

unit-testing - 如何在不使用 Maven 的情况下为使用 apache-cxf 和 spring 开发的 REST Web 服务编写单元测试

python - Mac 用 brew 安装 python3.6

python - python while 循环 : even when sleeping 97% of the time. 的 CPU 使用率高为什么?

python - 如何增加 tkinter(Python) 中输入(显示)框的大小(高度)?

Python模拟paramiko sftpclient打开

python-3.x - 为什么在循环中使用后无法访问选择 1

python - 如何识别某个图像何时消失

android - 使用 Gradle 在 Android Studio 中运行普通 Java 项目的单元测试