Skip to content

Commit

Permalink
Add cachedResult method to TestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Feb 9, 2024
1 parent a037c2e commit eb452c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/unittest_extensions/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def result(self) -> Any:
decorator.
"""
try:
return self.subject(**self._subjectKwargs)
self._subject_result = self.subject(**self._subjectKwargs)
return self._subject_result
except TypeError as e:
msg = e.args[0]
if "unexpected keyword argument" in msg:
Expand All @@ -60,6 +61,14 @@ def result(self) -> Any:
)
raise e

def cachedResult(self) -> Any:
"""
Return the result of the last `subject` call.
Use this function if when you to assert different attributes of your
subject without executing it multiple times.
"""
return self._subject_result

def assertResult(self, value):
"""
Fail if the result is unequal to the value as determined by the '=='
Expand Down
12 changes: 12 additions & 0 deletions src/unittest_extensions/tests/test_use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@ def test_change_state(self):
def test_change_state_twice(self):
self.assertResult(2)
self.assertResult(3)

def test_change_state_cached_result(self):
self.result()
self.assertEqual(self.cachedResult(), 2)
self.result()
self.assertEqual(self.cachedResult(), 3)

def test_manually_change_state_cached_result(self):
self.result()
self.assertEqual(self.cachedResult(), 2)
self.instance.state_var += 1
self.assertEqual(self.cachedResult(), 2)

0 comments on commit eb452c7

Please sign in to comment.