Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Improved Python examples, and added some Tutorial text to the main __…

…init__ file

git-svn-id: 2aa0e9f1-8e24-0410-adc4-1b6a3a6e65e2
  • Loading branch information...
commit d095577a62754c34686029d12b04c0d0e74b91cf 1 parent 3452475
@jonreid jonreid authored
7 examples/
@@ -2,10 +2,11 @@
from hamcrest.core.base_matcher import BaseMatcher
-import datetime
+from hamcrest.core.helpers.hasmethod import hasmethod
from hamcrest import *
import unittest
+import datetime
class IsGivenDayOfWeek(BaseMatcher):
@@ -16,9 +17,9 @@ def __init__(self, day):
def _matches(self, item):
"""Test whether item matches."""
- if not isinstance(item,
+ if not hasmethod(item, 'weekday'):
return False
- return ==
+ return item.weekday() ==
def describe_to(self, description):
"""Describe the matcher."""
61 hamcrest/
@@ -1,3 +1,64 @@
+"""Hamcrest is a framework for writing matcher objects, allowing you to
+declaratively define "match" rules. There are a number of situations where
+matchers are invaluable, such as UI validation, or data filtering, but it is in
+the area of writing flexible tests that matchers are most commonly used. This
+tutorial shows you how to use Hamcrest for unit testing.
+When writing tests it is sometimes difficult to get the balance right between
+overspecifying the test (and making it brittle to changes), and not specifying
+enough (making the test less valuable since it continues to pass even when the
+thing being tested is broken). Having a tool that allows you to pick out
+precisely the aspect under test and describe the values it should have, to a
+controlled level of precision, helps greatly in writing tests that are "just
+right." Such tests fail when the behavior of the aspect under test deviates
+from the expected behavior, yet continue to pass when minor, unrelated changes
+to the behaviour are made.
+We'll start by writing a very simple PyUnit test, but instead of using PyUnit's
+assertEqual function, we'll use Hamcrest's assert_that construct and the
+standard set of matchers:
+from hamcrest import *
+import unittest
+class BiscuitTest(unittest.TestCase):
+ def testEquals(self):
+ theBiscuit = Biscuit('Ginger')
+ myBiscuit = Biscuit('Ginger')
+ assert_that(theBiscuit, equal_to(myBiscuit))
+if __name__ == '__main__':
+ unittest.main()
+The assert_that function is a stylized sentence for making a test assertion. In
+this example, the subject of the assertion is the object theBiscuit, which is
+the first method parameter. The second method parameter is a matcher for
+Biscuit objects, here a matcher that checks one object is equal to another
+using the Python == operator. The test passes since the Biscuit class defines
+an eq method.
+If you have more than one assertion in your test you can include an identifier
+for the tested value in the assertion:
+assert_that(theBiscuit.getChocolateChipCount(), equal_to(10), 'chocolate chips')
+assert_that(theBiscuit.getHazelnutCount(), equal_to(3), 'hazelnuts')
+As a convenience, assert_that can also be used to verify a boolean condition:
+assert_that(theBiscuit.isCooked(), 'cooked')
+This is equivalent to the assert_ method of unittest.TestCase, but because it's
+a standalone function, it offers greater flexibility in test writing.
+Further information is available from
__author__ = "Jon Reid"
__copyright__ = "Copyright 2010"
__license__ = "BSD, see License.txt"
4 hamcrest/core/
@@ -23,8 +23,8 @@ def assert_that(arg1, arg2, arg3=''):
assert_that can also verify a boolean condition:
assert_that(assertion, reason)
- This is equivalent to the unittest.TestCase.assert_ method, but because it
- is a standalone function, it offers greater flexibility in test writing.
+ This is equivalent to the assert_ method of unittest.TestCase, but because
+ it's a standalone function, it offers greater flexibility in test writing.
if isinstance(arg2, Matcher):
Please sign in to comment.
Something went wrong with that request. Please try again.