From b77c7c4def72d1095abbdf8a490842d55a38621c Mon Sep 17 00:00:00 2001 From: Maximos Nikiforakis Date: Fri, 9 Feb 2024 08:13:15 +0200 Subject: [PATCH 1/3] Add testing workflow to feature/* branches --- .github/workflows/test-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" ] From a037c2ec342a0f2b42215182b00a613584693b2b Mon Sep 17 00:00:00 2001 From: Maximos Nikiforakis Date: Fri, 9 Feb 2024 20:22:30 +0200 Subject: [PATCH 2/3] Add assertResultRaisesRegex method to TestCase --- src/unittest_extensions/case.py | 8 ++++++++ src/unittest_extensions/tests/test_use_case.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/unittest_extensions/case.py b/src/unittest_extensions/case.py index 7c73ce1..56732b6 100644 --- a/src/unittest_extensions/case.py +++ b/src/unittest_extensions/case.py @@ -138,6 +138,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..14ef048 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): From eb452c73457f00a5ca0aca87330fb8d4f35b2c61 Mon Sep 17 00:00:00 2001 From: Maximos Nikiforakis Date: Fri, 9 Feb 2024 20:23:08 +0200 Subject: [PATCH 3/3] Add cachedResult method to TestCase --- src/unittest_extensions/case.py | 11 ++++++++++- src/unittest_extensions/tests/test_use_case.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/unittest_extensions/case.py b/src/unittest_extensions/case.py index 56732b6..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 '==' diff --git a/src/unittest_extensions/tests/test_use_case.py b/src/unittest_extensions/tests/test_use_case.py index 14ef048..9f5a69b 100644 --- a/src/unittest_extensions/tests/test_use_case.py +++ b/src/unittest_extensions/tests/test_use_case.py @@ -123,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)