# Testing file IO errors in python

Below is a simple class with two methods: the first calls open() to read in a file. The second calls the first method and does error handling.

There is also a corresponding test class that tests the first methods returns data as expected and the other methods test the error handling of potential exceptions thrown by open.

Firstly, I would like to know if these tests are written in the appropriate manner using python3, mocking and patching as I am new to the language and especially to testing.

I also have a query to why mocking an exception to be thrown cannot be handled like the rest of the tests (see last test method)

fileMethods.py

# -*- coding: utf-8 -*-

try:
#   manipulation of filecontent
except FileNotFoundError:
filecontent = "FileNotFoundError"
except PermissionError:
filecontent = "PermissionError"
return filecontent

with open(myfile, 'r') as file:
file.close()
return filecontent


fileMethods_test.py

# -*- coding: utf-8 -*-

import io
import unittest
import unittest.mock as mock
import packageName.fileMethods

class TestIO(unittest.TestCase):

mock_file_content = io.StringIO('Content in file')
with mock.patch('packageName.fileMethods.open', return_value=mock_file_content, create=True):
self.assertEqual('Content in file', result)

self.assertEqual('string', result)

self.assertEqual('FileNotFoundError', result)

self.assertEqual('PermissionError', result)

# Does not work


The design of the readconfig() function is seriously problematic. It makes no sense to convert caught exceptions into data, as if the file literally contained the string "FileNotFoundError" or "PermissionError". If you don't have a good way to handle an exception, the right thing to do is let it propagate.

The readfile() function should be simpler:

def read_file(path):
with open(path) as file:

• myfile could be better named. It's a filesystem path, not a file handle. Also, who is "me" in this case?
• The 'r' mode is the default.
• You don't need to call file.close() on an already closed file. The whole point of the with block is that the file is automatically closed when exiting the block.
• Thanks for the feedback especially with my read_file method. I do not intent to return the error types of a string in an application. I was trying to understand how to test and account for exceptions thrown in a method/function which is used. If like you say I let the error propagate like i did in the read_file method how would I test that an error has been thrown? I attempted to do this in the last test method test_readconfig_throws_OSError which does not work. – SJC Aug 1 '16 at 18:55