Created
November 30, 2018 10:41
-
-
Save abhijitmamarde/e4895d7f3bd1b4cbd7c161519c95fdb7 to your computer and use it in GitHub Desktop.
creating mock of builtin functions in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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