Skip to content

Commit 56eb8fa

Browse files
committed
Merge branch 'feature/1-store-subject-result'
2 parents 580884b + eb452c7 commit 56eb8fa

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

.github/workflows/test-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
workflow_call:
55
workflow_dispatch:
66
push:
7-
branches: [ "main" ]
7+
branches: [ "main", "feature/*" ]
88
pull_request:
99
branches: [ "main" ]
1010

src/unittest_extensions/case.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def result(self) -> Any:
4343
decorator.
4444
"""
4545
try:
46-
return self.subject(**self._subjectKwargs)
46+
self._subject_result = self.subject(**self._subjectKwargs)
47+
return self._subject_result
4748
except TypeError as e:
4849
msg = e.args[0]
4950
if "unexpected keyword argument" in msg:
@@ -60,6 +61,14 @@ def result(self) -> Any:
6061
)
6162
raise e
6263

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+
6372
def assertResult(self, value):
6473
"""
6574
Fail if the result is unequal to the value as determined by the '=='
@@ -138,6 +147,14 @@ def assertResultRaises(self, expected_exception):
138147
with self.assertRaises(expected_exception):
139148
self.result()
140149

150+
def assertResultRaisesRegex(self, expected_exception, expected_regex):
151+
"""
152+
Fail unless an exception of class expected_exception is raised by the
153+
result and the message matches the regex.
154+
"""
155+
with self.assertRaisesRegex(expected_exception, expected_regex):
156+
self.result()
157+
141158
def assertResultAlmost(self, value, places=None, delta=None):
142159
"""
143160
Fail if the result is unequal to the value as determined by their

src/unittest_extensions/tests/test_use_case.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from unittest_extensions import TestCase, args
2+
from unittest_extensions.error import TestError
23

34

45
class TestClass:
@@ -49,6 +50,18 @@ def test_add_float_to_float(self):
4950
def test_add_str_to_str(self):
5051
self.assertResult("1-3-")
5152

53+
@args({"a": 1, "c": 2})
54+
def test_wrong_kwargs_raises(self):
55+
self.assertResultRaisesRegex(
56+
TestError, "Subject received an unexpected keyword argument."
57+
)
58+
59+
@args({"a": 1})
60+
def test_missing_arg_raises(self):
61+
self.assertResultRaisesRegex(
62+
TestError, "Subject misses 1 required positional argument."
63+
)
64+
5265

5366
class TestAppend(TestCase):
5467

@@ -110,3 +123,15 @@ def test_change_state(self):
110123
def test_change_state_twice(self):
111124
self.assertResult(2)
112125
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)

0 commit comments

Comments
 (0)