Create assert_that function and use it from TestCase.assertThat.

This will allow matchers to be used outside of the TestCase context.
This is useful for people who can't buy in to testtools.TestCase fully
(for whatever reason), or who like using function tests (as supported by
nose).
This commit is contained in:
Daniel Watkins
2013-11-30 11:10:07 +00:00
parent f57a7a8e50
commit 3ec5d538cc
2 changed files with 22 additions and 8 deletions

12
testtools/assertions.py Normal file
View File

@@ -0,0 +1,12 @@
from testtools.matchers import (
Annotate,
MismatchError,
)
def assert_that(matchee, matcher, message='', verbose=False):
matcher = Annotate.if_message(message, matcher)
mismatch = matcher.match(matchee)
if not mismatch:
return
raise MismatchError(matchee, matcher, mismatch, verbose)

View File

@@ -29,6 +29,9 @@ from extras import (
from testtools import (
content,
)
from testtools.assertions import (
assert_that,
)
from testtools.compat import (
advance_iterator,
reraise,
@@ -401,14 +404,13 @@ class TestCase(unittest.TestCase):
:param matcher: An object meeting the testtools.Matcher protocol.
:raises MismatchError: When matcher does not match thing.
"""
matcher = Annotate.if_message(message, matcher)
mismatch = matcher.match(matchee)
if not mismatch:
return
existing_details = self.getDetails()
for (name, content) in mismatch.get_details().items():
self.addDetailUniqueName(name, content)
raise MismatchError(matchee, matcher, mismatch, verbose)
try:
assert_that(matchee, matcher, message, verbose)
except MismatchError as error:
mismatch = error.mismatch
for (name, content) in mismatch.get_details().items():
self.addDetailUniqueName(name, content)
raise
def addDetailUniqueName(self, name, content_object):
"""Add a detail to the test, but ensure it's name is unique.