I am attempting to write a unit test for a function that calls the open
method on a pathlib.Path
. I am able to successfully mock the open
method without issue, but verifying the function is having the correct behavior is difficult. See the sample code below:
def test_my_function(self):
with patch.object(Path, 'open') as mock_open:
my_function(*args) # This function calls Path.open
When I introspect mock_open
and review the _mock_mock_calls
list, I am unable to find the string path of the file that is being written to. The call history looks like this:
[
call(mode='w'),
call().__enter__(),
call().__enter__().write('<file contents>'),
call().__enter__().flush(),
call().__exit__(None, None, None),
]
Is there a way to test what path is being opened when Path.open
is called?