Package: matchers
Highly reusable custom hamcrest matchersFunctionsassert_that_raisesAvailable matchersemptydate_iso (ISO 8601 formatted date string)iterablehas_lenhas_keysmatches_recallable_json_subset_ofsuperset_ofdisjoint_withxml matchersxml_documentxml_rootxml_elementxml_contains_elementxml_namespacedsoap_documentsoap_messageselenium matchersis_displayedInstallationpython setup.py installDependencespyHamcrestDocumentationassert_that_raisesfromhamcrestimport*frommatchersimportassert_that_raiseswithassert_that_raises(Warning):raiseWarning()withassert_that_raises(instance_of(Warning)):raiseWarning()withassert_that_raises(has_property('message',has_string(u'warning'))):raiseWarning(u'this is a warning')# this raises AssertionError: no Exception raisedwithassert_that_raises(NameError):raiseWarning()# {'exception': Warning(u'this is a warning')}withassert_that_raises(Warning)ascaptured:raiseWarning(u'this is a warning')printcaptured['exception']emptyfromhamcrestimport*frommatchersimportemptyassert_that(str(),is_(empty()))assert_that(set(),is_(empty()))assert_that(dict(),is_(empty()))assert_that(list(),is_(empty()))assert_that(tuple(),is_(empty()))assert_that(unicode(),is_(empty()))It’s smart enough to deal with iterators and generatorsassert_that(iter([]),is_(empty()))assert_that((iforiin[]),is_(empty()))date_iso (ISO 8601 formatted date string)fromhamcrestimport*frommatchersimportdate_isoassert_that('1988-10-04T06:15:00.230943Z',is_(date_iso()))iterablefromhamcrestimport*frommatchersimportiterableassert_that(list(),is_(iterable()))assert_that(dict(),is_(iterable()))assert_that(tuple(),is_(iterable()))assert_that(set(),is_(iterable()))assert_that(str(),is_(iterable()))assert_that(unicode(),is_(iterable()))assert_that((iforiin[]),is_(iterable()))assert_that(iter([]),is_(iterable()))classIterateMe(object):l=list()def__iter__(self):returniter(l)assert_that(IterateMe(),is_(iterable()))has_lenReimplementation of has_length made to work with generators as wellfromhamcrestimport*frommatchersimporthas_lenassert_that((iforiin[1,2,3]),has_len(3))has_keysfromhamcrestimport*frommatchersimporthas_keysdictionary={'first':'foo','second':'bar'}assert_that(dictionary,has_keys(['first','second']))matches_refromhamcrestimport*frommatchersimportmatches_reassert_that('pattern',matches_re(r'pattern'))callable_fromhamcrestimport*frommatchersimportcallable_assert_that(lambda:'foo',is_(callable_()))json_fromhamcrestimport*frommatchersimportjson_assert_that("{'foo': ['bar']}",is_(json_()))assert_that("{'foo': ['bar']}",is_(json_(has_key('foo'))))subset_offromhamcrestimport*frommatchersimportsubset_ofassert_that([1,2],is_(subset_of([1,2,3])))superset_offromhamcrestimport*frommatchersimportsuperset_ofassert_that([1,2,3],is_(superset_of([1,2])))disjoint_withfromhamcrestimport*frommatchersimportdisjoint_withassert_that([1,2,3],is_(disjoint_with([4,5,6])))xml_documentfromhamcrestimport*frommatchersimportxml_documentfromxml.etreeimportElementassert_that('<element/>',is_(xml_document()))assert_that('<element/>',is_(xml_document(instance_of(Element))))xml_rootfromhamcrestimport*frommatchersimportxml_rootassert_that('<element/>',xml_root(tag='element'))xml_elementfromhamcrestimport*frommatchersimportxml_document,xml_elementassert_that('<element/>',is_(xml_element('element')))assert_that('<element/>',is_(xml_element('element',another_matcher)))assert_that('<foo:element/>',is_(xml_element(tag='element',ns='foo')))xml_contains_elementfromhamcrestimport*frommatchersimportxml_root,xml_element,xml_contains_elementassert_that('<parent><child/></parent>',is_(xml_element('parent',xml_contains_element('child'))))assert_that('<parent><child/></parent>',xml_root(is_(xml_element('parent',xml_contains_element('child')))))xml_namespacedfromhamcrestimport*frommatchersimportxml_namespacedassert_that('<element xmlns="http://foo.com"/>',is_(xml_namespaced('http://foo.com')))soap_documentfromhamcrestimport*frommatchersimportxml_document,soap_documentns_url="http://schemas.xmlsoap.org/soap/envelope/"string="<Envelope xmlns='"+ns_url+"' />"assert_that(string,is_(xml_document(is_(soap_document()))))soap_messagefromhamcrestimport*frommatchersimportxml_document,soap_document,soap_messagens_url="http://schemas.xmlsoap.org/soap/envelope/"string="""
    <Envelope xmlns='"""+ns_url+"""' >"
        <Body/>
    </Envelope>
"""assert_that(string,is_(xml_document(is_(soap_document(is_(soap_message()))))))is_displayedfromhamcrestimport*frommatchersimportis_displayedfromseleniumimportwebdriverbrowser=webdriver.Firefox()browser.open('wwww.google.com')logo=browser.find_element_by_css_selector('#hplogo')assert_that(logo,is_displayed())