1Writing Custom Matchers 2======================= 3 4PyHamcrest comes bundled with lots of useful matchers, but you'll probably find 5that you need to create your own from time to time to fit your testing needs. 6This commonly occurs when you find a fragment of code that tests the same set 7of properties over and over again (and in different tests), and you want to 8bundle the fragment into a single assertion. By writing your own matcher you'll 9eliminate code duplication and make your tests more readable! 10 11Let's write our own matcher for testing if a calendar date falls on a Saturday. 12This is the test we want to write:: 13 14 def testDateIsOnASaturday(self): 15 d = datetime.date(2008, 04, 26) 16 assert_that(d, is_(on_a_saturday())) 17 18And here's the implementation:: 19 20 from hamcrest.core.base_matcher import BaseMatcher 21 from hamcrest.core.helpers.hasmethod import hasmethod 22 23 class IsGivenDayOfWeek(BaseMatcher): 24 25 def __init__(self, day): 26 self.day = day # Monday is 0, Sunday is 6 27 28 def _matches(self, item): 29 if not hasmethod(item, 'weekday'): 30 return False 31 return item.weekday() == self.day 32 33 def describe_to(self, description): 34 day_as_string = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 35 'Friday', 'Saturday', 'Sunday'] 36 description.append_text('calendar date falling on ') \ 37 .append_text(day_as_string[self.day]) 38 39 def on_a_saturday(): 40 return IsGivenDayOfWeek(5) 41 42For our Matcher implementation we implement the 43:py:meth:`~hamcrest.core.base_matcher.BaseMatcher._matches` method - which 44calls the ``weekday`` method after confirming that the argument (which may not 45be a date) has such a method - and the 46:py:func:`~hamcrest.core.selfdescribing.SelfDescribing.describe_to` method - 47which is used to produce a failure message when a test fails. Here's an example 48of how the failure message looks:: 49 50 assert_that(datetime.date(2008, 04, 06), is_(on_a_saturday())) 51 52fails with the message:: 53 54 AssertionError: 55 Expected: is calendar date falling on Saturday 56 got: <2008-04-06> 57 58Let's say this matcher is saved in a module named ``isgivendayofweek``. We 59could use it in our test by importing the factory function ``on_a_saturday``:: 60 61 from hamcrest import * 62 import unittest 63 from isgivendayofweek import on_a_saturday 64 65 class DateTest(unittest.TestCase): 66 def testDateIsOnASaturday(self): 67 d = datetime.date(2008, 04, 26) 68 assert_that(d, is_(on_a_saturday())) 69 70 if __name__ == '__main__': 71 unittest.main() 72 73Even though the ``on_a_saturday`` function creates a new matcher each time it 74is called, you should not assume this is the only usage pattern for your 75matcher. Therefore you should make sure your matcher is stateless, so a single 76instance can be reused between matches. 77 78If you need your matcher to provide more details in case of a mismatch, you 79can override the :py:meth:`~hamcrest.core.base_matcher.BaseMatcher.describe_mismatch` 80method. For example, if we added this 81:py:meth:`~hamcrest.core.base_matcher.BaseMatcher.describe_mismatch` implementation 82to our ``IsGivenDayOfWeek`` matcher:: 83 84 def describe_mismatch(self, item, mismatch_description): 85 day_as_string = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 86 'Friday', 'Saturday', 'Sunday'] 87 mismatch_description.append_text('got ') \ 88 .append_description_of(item) \ 89 .append_text(' which is a ') \ 90 .append_text(day_as_string[item.weekday()]) 91 92Our matcher would now give the message:: 93 94 AssertionError: 95 Expected: is calendar date falling on Saturday 96 got: <2008-04-06> which is a Sunday 97 98 99Occasionally, you might also need your matcher to explain why it matched successfully. 100For example, if your matcher is wrapped by a :py:meth:`~hamcrest.core.core.isnot.is_not` 101matcher, the ``is_not`` matcher can only explain its mismatches by understanding why your 102matcher succeeded. In this case, your matcher can implement 103:py:meth:`~hamcrest.core.base_matcher.BaseMatcher.describe_match`. 104