Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added self.assertMatches(matcher, val, [msg]) to mocktest.TestCase

  • Loading branch information...
commit aa5cb301ded92879de6c70e01c978b92cbdb1740 1 parent d3ec3a4
@gfxmonk authored
Showing with 40 additions and 0 deletions.
  1. +11 −0 mocktest/mocktest.py
  2. +29 −0 test/mocktest_test.py
View
11 mocktest/mocktest.py
@@ -127,6 +127,7 @@ class TestCase(unittest.TestCase):
order to reset mock state and verify expectations upon test \
completion.
- enhanced versions of assertTrue / False, assertRaises
+ - assertMatches
"""
pending = globals()['pending']
ignore = globals()['ignore']
@@ -267,6 +268,16 @@ def assertRaises(self, exception, func, message = None, args = None, kwargs = No
else:
self.fail("%s did not raise an exception" % (callsig,))
failUnlessRaises = assertRaises
+
+ def assertMatches(self, matcher, val, message=None):
+ """
+ Fail the test if an object does not satisfy the given matcher.
+ """
+ if not matcher.matches(val):
+ fail_msg = "expected:\n%r\nto be %s" % (val, matcher.desc())
+ if message is not None:
+ fail_msg += "\n(%s)" % (message,)
+ self.fail(fail_msg)
def run(self, result=None):
"""
View
29 test/mocktest_test.py
@@ -303,6 +303,35 @@ def test_raise_mismatch_regex(s):
result = self.run_method(test_raise_mismatch_regex)
self.assertFalse(result.wasSuccessful())
+ def test_assert_matches_success(self):
+ from mocktest.matchers import string_matching
+ def test_string_match(s):
+ s.assertMatches(string_matching('^f'), 'foo')
+ result = self.run_method(test_string_match)
+ self.assertTrue(result.wasSuccessful())
+
+ def test_assert_matches_error_message(self):
+ from mocktest.matchers import string_matching
+ def test_string_match(s):
+ s.assertMatches(string_matching('^f'), 'boo')
+ result = self.run_method(test_string_match)
+ self.assertFalse(result.wasSuccessful())
+ failure_text = result.failures[0][1]
+ self.assertTrue(
+ """AssertionError: expected:\n'boo'\nto be a string matching: ^f""" in failure_text,
+ repr(result))
+
+ def test_assert_matches_error_message_with_custom_message(self):
+ from mocktest.matchers import string_matching
+ def test_string_match(s):
+ s.assertMatches(string_matching('^f'), 'boo', 'message')
+ result = self.run_method(test_string_match)
+ self.assertFalse(result.wasSuccessful())
+ failure_text = result.failures[0][1]
+ self.assertTrue(
+ """AssertionError: expected:\n'boo'\nto be a string matching: ^f\n(message)""" in failure_text,
+ repr(result))
+
def test_reality_formatting(self):
core._teardown()
try:
Please sign in to comment.
Something went wrong with that request. Please try again.