Skip to content

Commit 8d43bbe

Browse files
committed
Catch errors correctly in result
1 parent abc9e1b commit 8d43bbe

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/unittest_extensions/case.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ def result(self) -> Any:
4545
try:
4646
self._subject_result = self.subject(**self._subjectKwargs)
4747
return self._subject_result
48-
except TypeError as e:
48+
except Exception as e:
4949
msg = e.args[0]
50-
if "unexpected keyword argument" in msg:
50+
if "subject() got an unexpected keyword argument" in msg:
5151
raise TestError(
5252
"Subject received "
5353
+ msg.split("subject() got ")[1]
5454
+ ". Did you decorate a test method with the wrong 'args'?"
5555
)
56-
elif "required positional argument" in msg:
56+
elif "subject() missing" in msg and ("required positional argument" in msg):
5757
raise TestError(
5858
"Subject misses "
5959
+ msg.split("subject() missing ")[1]

src/unittest_extensions/tests/test_use_case.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,23 @@ def test_missing_arg_raises(self):
6464

6565
@args({"a": 1, "b": 2})
6666
def test_cachedResult_raises(self):
67-
with self.assertRaises(TestError):
67+
with self.assertRaisesRegex(
68+
TestError, "Cannot call 'cachedResult' before calling 'result'"
69+
):
6870
self.cachedResult()
6971

7072

73+
class TestSubjectMissingRequiredPositionalArguments(TestCase):
74+
def subject(self, a, b, c, d):
75+
return 1
76+
77+
@args({"a": 1, "b": 2})
78+
def test_raises_test_error(self):
79+
self.assertResultRaisesRegex(
80+
TestError, "Subject misses 2 required positional arguments"
81+
)
82+
83+
7184
class TestAppend(TestCase):
7285

7386
def instance(self) -> TestClass:

0 commit comments

Comments
 (0)