Patch builtins.open so that it returns StringIO object. :param side_effect: Additional side effect for when the open context is entered. Example:: >>> with mock.open(io.BytesIO) as open_fh: ... something_opening_and_writing_bytes_to_a_file() ... self.a
(side_effect=None)
| 716 | |
| 717 | @contextmanager |
| 718 | def open(side_effect=None): |
| 719 | """Patch builtins.open so that it returns StringIO object. |
| 720 | :param side_effect: Additional side effect for when the open context |
| 721 | is entered. |
| 722 | Example:: |
| 723 | >>> with mock.open(io.BytesIO) as open_fh: |
| 724 | ... something_opening_and_writing_bytes_to_a_file() |
| 725 | ... self.assertIn(b'foo', open_fh.getvalue()) |
| 726 | """ |
| 727 | with patch('builtins.open') as open_: |
| 728 | with _mock_context(open_) as context: |
| 729 | if side_effect is not None: |
| 730 | context.__enter__.side_effect = side_effect |
| 731 | val = context.__enter__.return_value = WhateverIO() |
| 732 | val.__exit__ = Mock() |
| 733 | yield val |
| 734 | |
| 735 | |
| 736 | @contextmanager |
no test coverage detected