Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created November 30, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhijitmamarde/e4895d7f3bd1b4cbd7c161519c95fdb7 to your computer and use it in GitHub Desktop.
Save abhijitmamarde/e4895d7f3bd1b4cbd7c161519c95fdb7 to your computer and use it in GitHub Desktop.
creating mock of builtin functions in Python
from unittest.mock import Mock
import builtins
len = builtins.len
def new_len(o):
return builtins.len(o) + 1
def patch_len():
global len
len = Mock()
len.side_effect = new_len
def test_new_len():
length = len("a")
assert len.call_count == 1
assert length == 2
length = len("abcd")
assert len.call_count == 2
assert length == 5
print("Success!")
if __name__ == "__main__":
patch_len()
test_new_len()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment