File tree Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Expand file tree Collapse file tree 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:
8181 subject without executing it multiple times.
8282
8383 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.
8487 """
8588 if not hasattr (self , "_subject_result" ):
8689 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 )
8893
8994 def assertResult (self , value ):
9095 """
Original file line number Diff line number Diff line change @@ -182,3 +182,14 @@ def subject(self, lst):
182182 def test_mutable_kwargs (self ):
183183 self .subjectKwargs ()["lst" ].append (3 )
184184 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