File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,8 @@ def result(self) -> Any:
43
43
decorator.
44
44
"""
45
45
try :
46
- return self .subject (** self ._subjectKwargs )
46
+ self ._subject_result = self .subject (** self ._subjectKwargs )
47
+ return self ._subject_result
47
48
except TypeError as e :
48
49
msg = e .args [0 ]
49
50
if "unexpected keyword argument" in msg :
@@ -60,6 +61,14 @@ def result(self) -> Any:
60
61
)
61
62
raise e
62
63
64
+ def cachedResult (self ) -> Any :
65
+ """
66
+ Return the result of the last `subject` call.
67
+ Use this function if when you to assert different attributes of your
68
+ subject without executing it multiple times.
69
+ """
70
+ return self ._subject_result
71
+
63
72
def assertResult (self , value ):
64
73
"""
65
74
Fail if the result is unequal to the value as determined by the '=='
Original file line number Diff line number Diff line change @@ -123,3 +123,15 @@ def test_change_state(self):
123
123
def test_change_state_twice (self ):
124
124
self .assertResult (2 )
125
125
self .assertResult (3 )
126
+
127
+ def test_change_state_cached_result (self ):
128
+ self .result ()
129
+ self .assertEqual (self .cachedResult (), 2 )
130
+ self .result ()
131
+ self .assertEqual (self .cachedResult (), 3 )
132
+
133
+ def test_manually_change_state_cached_result (self ):
134
+ self .result ()
135
+ self .assertEqual (self .cachedResult (), 2 )
136
+ self .instance .state_var += 1
137
+ self .assertEqual (self .cachedResult (), 2 )
You can’t perform that action at this time.
0 commit comments