Mocking Pathlib write_text?

  mocking, python

I’m writing a simple function to write out a dict to disk via some transformation and yaml. The easiest way to test what’s going on would be to capture the output of Path(file_name).write_text(text_to_write). But I’m having trouble understanding exactly what I need to do to capture this content.

def test_path_write():
    with mock.patch('Path.write_text', return_value=None):
        function_that_uses_path_write_text()
        assert 'Content I'd like to be in the file' in SOME_KIND_OF_CAPTURE

Any idea how I get something into the SOME_KIND_OF_CAPTURE that I can use to test against?

Source: Python Questions

LEAVE A COMMENT