dimanche 31 juillet 2016

Assert Error is thrown when mocking python3

I am trying to write a test that mocks raising PermissionError on a call to open() when attempting to open a file for reading. However I cannot seem to get the test working. The PermissionError appears to be thrown but my test fails because of this even though I am trying to assert it is thrown.

Below contains one of my attempts:

fileMethods.py

def readfile(myfile):
    with open(myfile, 'r') as file:
        filecontent = file.read()
    file.close()
    return filecontent

fileMethods_test.py

def test_readfile_throws_PermissionError(self):
    with mock.patch('fileMethods.open') as openMock:
        openMock.side_effect = PermissionError
        self.assertRaises(PermissionError, fileMethods.readfile('file_to_readin'))

Am I missing something obvious or is the way I am testing this method incorrect?

Aucun commentaire:

Enregistrer un commentaire