Last active
July 3, 2018 07:40
-
-
Save ShinJJang/c60833e70aad07ca2816020fa2918e29 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ ./python3 test_mock_open.py .. ---------------------------------------------------------------------- Ran 2 tests in 0.010s OK