It's a basic thing, but since it fits a little, I recorded it as a memorandum
If you need a temporary file when writing a test etc. tempfile Is recommended.
tempfile.mkstemp () creates a temporary file, but you have to delete the file yourself.
I thought I should do ʻos.remove () after the file operation, and as a result of writing and executing it, the PermissionError: [WinError 32] process cannot access the file. Another process is in use. :'C: \ ... [Temporary file path] `'error occurred.
As a result of trying various things while thinking that the with syntax is missing, it seems that the process at runtime still grabs the file, so I need to do ʻos.close ()`. did.
Below is an example of a test that worked well
class MyFileController():
    def __init__(self, path):
        self.path = path
    
    def output(self, data):
        with open(self.path, "a", newline='', encoding='utf_8_sig', errors='ignore') as f:
            f.write(data)   
class TestMyFileController(unittest.TestCase):
    def test_output(self):
        fd, path  = tempfile.mkstemp()
        mcc = MyFileController(path)
        try:
            mcc.output("test")
            with open(path, encoding='utf_8_sig') as f:
                test_data = f.read()
                self.assertEqual(headers, "test")
        finally:
            os.close(fd)          # <--Here.
            os.remove(path)