I am writing a unit test for a function in my script, however I am having trouble creating a mock file. My function takes in a filename and output's it's md5 value.
def md5(file_name):
#Function to return the md5 value of a file
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
My unittest at the moment:
import mock
class Function_to_test_TestCase(unittest.TestCase):
def test_filename(self):
with mock.patch('__main__.open', mock.mock_open(read_data=''), create=True) as m:
md5_value=my_script.function_to_get_md5_value(m)
However I get the error:
with open(fname, "rb") as f:
TypeError: coercing to Unicode: need string or buffer, MagicMock found
Is this way the correct way to make a mock file with a related mock md5 value? Any advice is appreciated!