I implemented a test that reads data from temp file on Windows.
I originally had an issue that I solved with the help of this answer
def test_read(self): """Test read from file""" import tempfile import os try: with tempfile.NamedTemporaryFile(delete=False) as temp: content = 'Some data' temp.write(content.encode('utf-8')) temp.flush() self.assertEqual(content, b.read(Path(temp.name))) finally: os.remove(temp.name)
How can I do this better?
Is it ok to have
try...finally in the test?
b is a custom module.
It reads data from temp file.
def read(path): """Reads file content from FS""" if path in VIRTUAL_FS: return VIRTUAL_FS[path] with open(path.as_posix(), 'r') as file: return file.read()