Last active
March 15, 2017 09:34
-
-
Save LiamJolly/62016dec527693364639fdc99e119672 to your computer and use it in GitHub Desktop.
Unit tests for file_utils.py, demontrating mock_open.
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
import unittest | |
import mock | |
import file_utils | |
class FileUtilsTest(unittest.TestCase): | |
def test_read_file(self): | |
with mock.patch("__builtin__.open", mock.mock_open(read_data="MOCKED"), create=True) as mock_file: | |
result = file_utils.read_file("path") | |
mock_file.assert_called_once_with("path", "r") | |
assert result == "MOCKED" | |
def test_write_file(self): | |
with mock.patch("__builtin__.open", mock.mock_open(), create=True) as mock_file: | |
file_utils.write_file("path", "content") | |
mock_file.assert_called_once_with("path", "w") | |
mock_file.return_value.write.assert_called_once_with("content") | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment