Created
November 30, 2018 11:22
-
-
Save abhijitmamarde/9680e8abbd42c4cb8a73b6568ba56dd7 to your computer and use it in GitHub Desktop.
mock method from standard package
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 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