Skip to content

Commit

Permalink
Merge branch 'feature/1-store-subject-result'
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Feb 9, 2024
2 parents 580884b + eb452c7 commit 56eb8fa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_call:
workflow_dispatch:
push:
branches: [ "main" ]
branches: [ "main", "feature/*" ]
pull_request:
branches: [ "main" ]

Expand Down
19 changes: 18 additions & 1 deletion src/unittest_extensions/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 '=='
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/unittest_extensions/tests/test_use_case.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest_extensions import TestCase, args
from unittest_extensions.error import TestError


class TestClass:
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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)

0 comments on commit 56eb8fa

Please sign in to comment.