高度可重复使用的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())

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
playframework Play Framework 2.5表单对象bean(Java)中的依赖项注入   java从javascript触发小程序按钮   JavaGWT:如何创建我自己的异步方法?   对于JTA事务类型,java JPA更改不会持久化   java如何使用预定义的甲板类制作Black Jack游戏   带有数组的java HashMap find()   从Java中的另一个类接收对象数组的参数?   java IntelliJ错误:Scala 145,错误:scalac:找不到Scala编译器JAR   java Eclipse 404请求的资源不可用   java Chuck没有界限   java阅读行每隔一行跳过一行   java如何访问我的EAR部署信息   Java中的正则表达式,其中执行搜索的文本是动态变化的   java我需要优化BigInteger的toString()方法   java Google Map Api V2,自定义路线标记   java如何在Java8中与LocalDate实现更少的需求和更高的质量   EclipseUML插件生成Java源代码   将数据库列绑定到java jComboBox和JList   java如何将两个图像合并为PDF