File tree Expand file tree Collapse file tree 1 file changed +3
-1
lines changed Expand file tree Collapse file tree 1 file changed +3
-1
lines changed Original file line number Diff line number Diff line change 5
5
In order to unit test this type of situation properly we don't want to create any file on disk.
6
6
To do this requires mocking the `open` builtin.
7
7
8
- For simple cases we can use unittest.mock.patch along with unittest.mock.mock_open and we get all we need.
8
+ If we need to deal with context managers for simple cases we can use unittest.mock.patch along with unittest.mock.mock_open and we get all we need.
9
9
10
10
However if we have to iterate over the mocked `open` we have to do more work.
11
11
For example say we have the following code that reads a file line by line
21
21
There's some extra work that has to be done if iteration over the mocked
22
22
file object is to be supported because this doesn't come as a default.
23
23
Specifically we much define `__iter__` for the `mock_open function.
24
+
25
+ The test case in this file will show you how you can do that.
24
26
"""
25
27
26
28
from mocking_file_opens import example_function
You can’t perform that action at this time.
0 commit comments