Skip to content

Commit

Permalink
add Defer Assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
nltd101 committed Jan 8, 2025
1 parent 346d3f7 commit 11f0479
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
17 changes: 17 additions & 0 deletions examples/ExampleWithDeferAssert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys

sys.path.append("..")

from hamcrest import *
import unittest


class ExampleWithDeferAssert(unittest.TestCase):
def testUsingDeferAssertThat(self):
with DeferAssertContextManager() as da:
da.assert_that("xx", is_("xx"))



if __name__ == "__main__":
unittest.main()
3 changes: 2 additions & 1 deletion src/hamcrest/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ruff: noqa: F405, F403

from hamcrest.core.assert_that import assert_that
from hamcrest.core.assert_that import assert_that, DeferAssertContextManager
from hamcrest.core.core import *

__author__ = "Jon Reid"
Expand All @@ -23,4 +23,5 @@
"not_none",
"raises",
"same_instance",
"DeferAssertContextManager"
]
16 changes: 16 additions & 0 deletions src/hamcrest/core/assert_that.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ def _assert_bool(assertion: bool, reason: Optional[str] = None) -> None:
if not reason:
reason = "Assertion failed"
raise AssertionError(reason)

class DeferAssertContextManager:
def __init__(self):
self.exceptions = []

def assert_that(self, *args, **kwargs):
try:
assert_that(*args, **kwargs)
except AssertionError as e:
self.exceptions.append(e)
def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
if len(self.exceptions) > 0:
raise self.exceptions.pop(0)
52 changes: 52 additions & 0 deletions tests/hamcrest_unit_test/defer_assert_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# encoding: utf-8
import unittest

from hamcrest.core.assert_that import DeferAssertContextManager
from hamcrest.core.core.isequal import equal_to

class DeferAssertContextManagerTest(unittest.TestCase):
def testAssertionSuccessfully(self):
with DeferAssertContextManager() as da:
da.assert_that(1, equal_to(1))

def testAssertionTeardownSuccessfully(self):
actual = "ACTUAL"

with DeferAssertContextManager() as da:
da.assert_that(actual, equal_to("ACTUAL"))
actual = ""
self.assertEqual(actual, "")

def testAssertionErrorShouldTeardownBeforeRaiseExeption(self):
self.maxDiff = None
expected = "EXPECTED"
actual = "ACTUAL"

expectedMessage = "\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n"

with self.assertRaises(AssertionError) as e:
with DeferAssertContextManager() as da:
da.assert_that(actual, equal_to(expected))
actual = ""
self.assertEqual(actual, "")
self.assertEqual(expectedMessage, str(e.exception))


def testAssertionErrorShouldRaiseExceptionBeforeExitingContext(self):
self.maxDiff = None
expected = "EXPECTED"
actual = "ACTUAL"

expectedMessage = "\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n"

with self.assertRaises(AssertionError) as e:
with DeferAssertContextManager() as da:
da.assert_that(actual, equal_to(expected))
actual = ""
self.assertNotEqual(actual, "")
self.assertEqual(expectedMessage, str(e.exception))



if __name__ == "__main__":
unittest.main()

0 comments on commit 11f0479

Please sign in to comment.