File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -360,6 +360,30 @@ of arbitrary attributes as well as the getting of them then you can use
360360*spec_set * instead of *spec *.
361361
362362
363+ Using side_effect to return per file content
364+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
365+
366+ :func: `mock_open ` is used to patch :func: `open ` method. :attr: `~Mock.side_effect `
367+ can be used to return a new Mock object per call. This can be used to return different
368+ contents per file stored in a dictionary::
369+
370+ DEFAULT = "default"
371+ data_dict = {"file1": "data1",
372+ "file2": "data2"}
373+
374+ def open_side_effect(name):
375+ return mock_open(read_data=data_dict.get(name, DEFAULT))()
376+
377+ with patch("builtins.open", side_effect=open_side_effect):
378+ with open("file1") as file1:
379+ assert file1.read() == "data1"
380+
381+ with open("file2") as file2:
382+ assert file2.read() == "data2"
383+
384+ with open("file3") as file2:
385+ assert file2.read() == "default"
386+
363387
364388Patch Decorators
365389----------------
You can’t perform that action at this time.
0 commit comments