Skip to content

Instantly share code, notes, and snippets.

@ShinJJang
Last active July 3, 2018 07:40
Show Gist options
  • Save ShinJJang/c60833e70aad07ca2816020fa2918e29 to your computer and use it in GitHub Desktop.
Save ShinJJang/c60833e70aad07ca2816020fa2918e29 to your computer and use it in GitHub Desktop.
import os
import unittest
from unittest import mock
class TestMockOpen(unittest.TestCase):
def test_mock_open(self):
mock_open = mock.mock_open()
with mock.patch('builtins.open', new_callable=mock_open()):
with open('temp.file', 'w') as f:
f.write('temp temp!')
self.assertFalse(os.path.exists('temp.file'))
self.assertFalse(os.path.exists('temp.file'))
def test_create_file(self):
with open('done.file', 'w') as f:
f.write('done!')
self.assertTrue((os.path.exists('done.file')))
if __name__ == '__main__':
unittest.main()
@ShinJJang
Copy link
Author

ShinJJang commented Jul 3, 2018

$ ./python3 test_mock_open.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.010s

OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment