Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created November 30, 2018 11:22
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/9680e8abbd42c4cb8a73b6568ba56dd7 to your computer and use it in GitHub Desktop.
Save abhijitmamarde/9680e8abbd42c4cb8a73b6568ba56dd7 to your computer and use it in GitHub Desktop.
mock method from standard package
from os.path import exists as orig_exists
from unittest.mock import Mock
exists = orig_exists
def new_exists(file):
print(f"new_exists: checking if '{file}' exists:", orig_exists(file))
print("but will send exists = True")
return True
def patch_exists():
global exists
exists = Mock()
exists.side_effect = new_exists
def test_file_exists(file):
rv = exists(file)
assert rv == True
def test_file_not_exists(file):
rv = exists(file)
assert rv == False
if __name__ == "__main__":
file = "d:/non_existing_file.txt"
test_file_not_exists(file)
patch_exists()
test_file_exists(file)
print("Success!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment