File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed 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
360
360
*spec_set * instead of *spec *.
361
361
362
362
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
+
363
387
364
388
Patch Decorators
365
389
----------------
You can’t perform that action at this time.
0 commit comments