Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- from unittest import mock
- import sys
- def main():
- with open("path/to/file1", "rb") as f1:
- print(f1.read())
- with open("path/to/file2", "rb") as f2:
- print(f2.read())
- sys.exit(0)
- class MyUnitTest(unittest.TestCase):
- def test_main(self):
- read_data_map = {
- "path/to/file1": "read from f1",
- "path/to/file2": "read from f2",
- }
- mocker = mock.MagicMock(name="open", spec=open)
- mocker.side_effect = lambda *arg: mock.mock_open(
- read_data=read_data_map.get(arg[0])
- )()
- with self.assertRaises(SystemExit) as cm, mock.patch(
- "builtins.open", mocker
- ) as mock_files:
- main()
- self.assertListEqual(
- mock_files.call_args_list,
- [mock.call("path/to/file1", "rb"), mock.call("path/to/file2", "rb")],
- ),
- if __name__ == "__main__":
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement