File tree 2 files changed +17
-1
lines changed
2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -81,10 +81,15 @@ def cachedResult(self) -> Any:
81
81
subject without executing it multiple times.
82
82
83
83
Raises `unittest_extensions.TestError` if subject has not been called.
84
+
85
+ The returned object is a copy of the result. Thus, the result cannot be
86
+ mutated by mutating the returned object of this method.
84
87
"""
85
88
if not hasattr (self , "_subject_result" ):
86
89
raise TestError ("Cannot call 'cachedResult' before calling 'result'" )
87
- return self ._subject_result
90
+ # NOTE: deepcopy keeps a reference of the copied object. This can cause
91
+ # issues with memory.
92
+ return deepcopy (self ._subject_result )
88
93
89
94
def assertResult (self , value ):
90
95
"""
Original file line number Diff line number Diff line change @@ -182,3 +182,14 @@ def subject(self, lst):
182
182
def test_mutable_kwargs (self ):
183
183
self .subjectKwargs ()["lst" ].append (3 )
184
184
self .assertDictEqual (self ._subjectKwargs , {"lst" : [1 , 2 ]})
185
+
186
+
187
+ class TestCachedResult (TestCase ):
188
+ def subject (self , lst ):
189
+ return lst
190
+
191
+ @args ({"lst" : [1 , 2 ]})
192
+ def test_mutate_cached_result (self ):
193
+ self .assertResult ([1 , 2 ])
194
+ self .cachedResult ().append (3 )
195
+ self .assertListEqual (self .cachedResult (), [1 , 2 ])
You can’t perform that action at this time.
0 commit comments