Skip to content

Instantly share code, notes, and snippets.

@itsmemattchung
Created January 30, 2016 09:27
Show Gist options
  • Save itsmemattchung/cda794287f2f0f48eca8 to your computer and use it in GitHub Desktop.
Save itsmemattchung/cda794287f2f0f48eca8 to your computer and use it in GitHub Desktop.
Example on mocking a tempfile
import tempfile
import mock
def use_tempfile():
dest_dir = '/tmp'
with tempfile.NamedTemporaryFile(dir=dest_dir, delete=False) as temp_dest:
print "hello world"
return temp_dest
@mock.patch.object(tempfile, 'NamedTemporaryFile')
def call_use_tempfile(temp_file):
return use_tempfile()
if __name__ == "__main__":
print call_use_tempfile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment