Package: open-mock-file
DescriptionThis library contains object, which can be used during mocking files in
tests. Features:You can register different actions (provide different objects) for
different pathsYou can add text, which will be returned on readYou can select behavior for paths, which are not registered.
Supported behaviors:Raise FileNotFoundError exception (default behavior)Return original file from filesystemReturn mock objectInstallIt can be installed with following command:pip installopen-mock-fileUsage:ExamplesMock open first file with custom object, second one with string data and
raise FileNotFoundError for not registered path:fromunittest.mockimportpatchfromopen_file_mockimportMockOpenclassFileObject:def__init__(self,data):self.data=datadefread(self,*args,**kwargs):returnself.datadefwrite(self,data,*args,**kwargs):self.data=datadef__enter__(self):returnselfdef__exit__(self,exc_type,exc_val,exc_tb):passwithpatch('builtins.open',new_callable=MockOpen)asopen_mock:open_mock.register_object_for_path(path='/tmp/f1',obj=FileObject('Some data'))open_mock.set_read_data_for(path='/tmp/f2',data='file2 data')withopen('/tmp/f1')asf1:print(f1.read())f1.write('Other data')print(f1.read())print('----------------------')withopen('/tmp/f2')asf2:print(f2.read())print('----------------------')open('/etc/hostname')output:Some data
Other data
----------------------
file2 data
----------------------
Traceback (most recent call last):
...
FileNotFoundError: File /etc/hostname not found in mock functionSet default behavior (for not registered paths) to return original file:fromunittest.mockimportpatch,MagicMockfromopen_file_mockimportMockOpen,DEFAULTS_ORIGINALwithpatch('builtins.open',new_callable=MockOpen)asopen_mock:open_mock.default_behavior=DEFAULTS_ORIGINALopen_mock.register_object_for_path(path='/tmp/f1',obj=MagicMock())withopen('/tmp/f1')asf1:print(f1.read())print('----------------------')withopen('/etc/hostname')asf2:print(f2.read())print('----------------------')output:<MagicMock name='mock.__enter__().read()' id='...'>
----------------------
myhost

----------------------Set default behavior to return new mock:fromunittest.mockimportpatchfromopen_file_mockimportMockOpen,DEFAULTS_MOCKwithpatch('builtins.open',new_callable=MockOpen)asopen_mock:open_mock.default_behavior=DEFAULTS_MOCKopen_mock.set_read_data_for('/tmp/f1','QWERTY')withopen('/tmp/f1')asf1:print(f1.read())print('----------------------')withopen('/etc/hostname')asf2:print(f2.read())print('----------------------')output:QWERTY
----------------------
<MagicMock name='mock.__enter__().read()' id='...'>
----------------------Yoy can get registered object withget_object_for_pathmethod:fromunittest.mockimportpatchfromopen_file_mockimportMockOpenclassFileObject:def__init__(self,data):self.data=datadefread(self,*args,**kwargs):returnself.datadefwrite(self,data,*args,**kwargs):self.data=datadef__enter__(self):returnselfdef__exit__(self,exc_type,exc_val,exc_tb):passwithpatch('builtins.open',new_callable=MockOpen)asopen_mock:open_mock.register_object_for_path(path='/tmp/f1',obj=FileObject('Some data'))open_mock.set_read_data_for(path='/tmp/f2',data='file2 data')withopen('/tmp/f1')asf1:print(f1.read())print('----------------------')print(open_mock.get_object_for_path('/tmp/f1'))output:Some data
----------------------
<__main__.FileObject object at ...>Object methods:register_object_for_path(path, obj)- allow to register new
object for provided path. Can be used once for particular pathupdate_object_for_path(path, obj)- update registered path or
create new mapping if not existsunregister_path(path)- remove path mappingset_read_data_for(path, data)- set data which will be
returned of file readget_object_for_path(path)- returns object registered for
path. If mapping not exists, raises KeyError