高度可重复使用的hamcrest火柴系列
matchers的Python项目详细描述
高度可重复使用的定制hamcrest匹配器
功能
- 断言
可用的匹配器
- 空
- 日期ISO(ISO 8601格式的日期字符串)
- Iterable
- 有长度
- 有U键
- 匹配项
- 可调用
- json 的子集
- 超集
- 与…分离
XML匹配器
- XML文档
- XML根目录
- XML元素
- XML包含元素
- XML命名空间
- soap_文档
- soap_消息
硒火柴
- 是否显示u
安装
python setup.py install
家属
- 皮哈姆克雷斯特
文件
断言
fromhamcrestimport*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']
空
fromhamcrestimport*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()))
它足够聪明,可以处理迭代器和生成器
assert_that(iter([]),is_(empty()))assert_that((iforiin[]),is_(empty()))
日期iso(iso 8601格式的日期字符串)
fromhamcrestimport*frommatchersimportdate_isoassert_that('1988-10-04T06:15:00.230943Z',is_(date_iso()))
Iterable
fromhamcrestimport*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_length,以便与发电机一起工作
fromhamcrestimport*frommatchersimporthas_lenassert_that((iforiin[1,2,3]),has_len(3))
有u键
fromhamcrestimport*frommatchersimporthas_keysdictionary={'first':'foo','second':'bar'}assert_that(dictionary,has_keys(['first','second']))
匹配项
fromhamcrestimport*frommatchersimportmatches_reassert_that('pattern',matches_re(r'pattern'))
可呼叫
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'))))
的子集
fromhamcrestimport*frommatchersimportsubset_ofassert_that([1,2],is_(subset_of([1,2,3])))
的超集
fromhamcrestimport*frommatchersimportsuperset_ofassert_that([1,2,3],is_(superset_of([1,2])))
与
不相交fromhamcrestimport*frommatchersimportdisjoint_withassert_that([1,2,3],is_(disjoint_with([4,5,6])))
XML文档
fromhamcrestimport*frommatchersimportxml_documentfromxml.etreeimportElementassert_that('<element/>',is_(xml_document()))assert_that('<element/>',is_(xml_document(instance_of(Element))))
xml根
fromhamcrestimport*frommatchersimportxml_rootassert_that('<element/>',xml_root(tag='element'))
XML元素
fromhamcrestimport*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包含元素
fromhamcrestimport*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命名空间
fromhamcrestimport*frommatchersimportxml_namespacedassert_that('<element xmlns="http://foo.com"/>',is_(xml_namespaced('http://foo.com')))
soap文件
fromhamcrestimport*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消息
fromhamcrestimport*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()))))))
是否显示
fromhamcrestimport*frommatchersimportis_displayedfromseleniumimportwebdriverbrowser=webdriver.Firefox()browser.open('wwww.google.com')logo=browser.find_element_by_css_selector('#hplogo')assert_that(logo,is_displayed())