Skip to content

Commit a037c2e

Browse files
committed
Add assertResultRaisesRegex method to TestCase
1 parent b77c7c4 commit a037c2e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/unittest_extensions/case.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ def assertResultRaises(self, expected_exception):
138138
with self.assertRaises(expected_exception):
139139
self.result()
140140

141+
def assertResultRaisesRegex(self, expected_exception, expected_regex):
142+
"""
143+
Fail unless an exception of class expected_exception is raised by the
144+
result and the message matches the regex.
145+
"""
146+
with self.assertRaisesRegex(expected_exception, expected_regex):
147+
self.result()
148+
141149
def assertResultAlmost(self, value, places=None, delta=None):
142150
"""
143151
Fail if the result is unequal to the value as determined by their

src/unittest_extensions/tests/test_use_case.py

Lines changed: 13 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

0 commit comments

Comments
 (0)