diff --git a/.github/workflows/test-package.yml b/.github/workflows/test-package.yml index f7e6304..c927203 100644 --- a/.github/workflows/test-package.yml +++ b/.github/workflows/test-package.yml @@ -4,7 +4,7 @@ on: workflow_call: workflow_dispatch: push: - branches: [ "main" ] + branches: [ "main", "feature/*" ] pull_request: branches: [ "main" ] diff --git a/src/unittest_extensions/case.py b/src/unittest_extensions/case.py index 7c73ce1..082377b 100644 --- a/src/unittest_extensions/case.py +++ b/src/unittest_extensions/case.py @@ -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: @@ -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 '==' @@ -138,6 +147,14 @@ def assertResultRaises(self, expected_exception): with self.assertRaises(expected_exception): self.result() + def assertResultRaisesRegex(self, expected_exception, expected_regex): + """ + Fail unless an exception of class expected_exception is raised by the + result and the message matches the regex. + """ + with self.assertRaisesRegex(expected_exception, expected_regex): + self.result() + def assertResultAlmost(self, value, places=None, delta=None): """ Fail if the result is unequal to the value as determined by their diff --git a/src/unittest_extensions/tests/test_use_case.py b/src/unittest_extensions/tests/test_use_case.py index 8a34548..9f5a69b 100644 --- a/src/unittest_extensions/tests/test_use_case.py +++ b/src/unittest_extensions/tests/test_use_case.py @@ -1,4 +1,5 @@ from unittest_extensions import TestCase, args +from unittest_extensions.error import TestError class TestClass: @@ -49,6 +50,18 @@ def test_add_float_to_float(self): def test_add_str_to_str(self): self.assertResult("1-3-") + @args({"a": 1, "c": 2}) + def test_wrong_kwargs_raises(self): + self.assertResultRaisesRegex( + TestError, "Subject received an unexpected keyword argument." + ) + + @args({"a": 1}) + def test_missing_arg_raises(self): + self.assertResultRaisesRegex( + TestError, "Subject misses 1 required positional argument." + ) + class TestAppend(TestCase): @@ -110,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)