Is it possible to mock the builtin len()
function in Python 3.6?
I have a class which defines a simple method whic is dependent upon the len()
function as follows:
class MyLenFunc(object):
def is_longer_than_three_characters(self, some_string):
return len(some_string) > 3
I am trying to write a unit test for this method, but I am unable to mock out the len()
function without errors being produced.
Here's what I have so far:
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()
I found another SO question/answer here which suggests that it is not possible to mock out builtin functions because they are immutable. However, I found a couple more websites here and here which suggest otherwise. My attempt at the unit test class above is taken directly from the latter of these websites (and yes, I've tried the other techniques mentioned there. All end with the same error).
The error I'm getting is rather long to post the full thing, so I'll snip out the repeating parts of it (you'll see that it's recursive from the final part of the error message). The error text is as follows:
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)
Many thanks in advance.
Don't patch builtins.len
; now code in the mock
library breaks, because it needs the function to operate as normal! Patch the globals of the module-under-test:
@patch('my_len_func.len')
This adds the global len
to your module, and the len(some_string)
in the MyLenFunc().is_longer_than_three_characters()
method will find that one rather than the built-in function.
However, I must say that testing if len()
is called feels like the wrong thing to test. You want to check if the method is producing the right results for the given inputs, basic operations like len()
are just small building blocks towards that goal and are usually not tested that extent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With