Skip to main content

Collection of highly reusable hamcrest matchers

Project description

Highly reusable custom hamcrest matchers

Functions

  • assert_that_raises

Available matchers

  • empty

  • date_iso (ISO 8601 formatted date string)

  • iterable

  • has_len

  • has_keys

  • matches_re

  • callable_

  • json_

  • subset_of

  • superset_of

  • disjoint_with

xml matchers

  • xml_document

  • xml_root

  • xml_element

  • xml_contains_element

  • xml_namespaced

  • soap_document

  • soap_message

selenium matchers

  • is_displayed

Installation

python setup.py install

Dependences

  • pyHamcrest

Documentation

assert_that_raises

from hamcrest import *
from matchers import assert_that_raises

with assert_that_raises(Warning):
    raise Warning()

with assert_that_raises(instance_of(Warning)):
    raise Warning()

with assert_that_raises(has_property('message', has_string(u'warning'))):
    raise Warning(u'this is a warning')

# this raises AssertionError: no Exception raised
with assert_that_raises(NameError):
    raise Warning()

# {'exception': Warning(u'this is a warning')}
with assert_that_raises(Warning) as captured:
    raise Warning(u'this is a warning')

print captured['exception']

empty

from hamcrest import *
from matchers import empty

assert_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 generators

assert_that(iter([]), is_(empty()))
assert_that((i for i in []), is_(empty()))

date_iso (ISO 8601 formatted date string)

from hamcrest import *
from matchers import date_iso

assert_that('1988-10-04T06:15:00.230943Z', is_(date_iso()))

iterable

from hamcrest import *
from matchers import iterable


assert_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((i for i in []), is_(iterable()))
assert_that(iter([]), is_(iterable()))

class IterateMe(object):
    l = list()
    def __iter__(self):
        return iter(l)

assert_that(IterateMe(), is_(iterable()))

has_len

Reimplementation of has_length made to work with generators as well

from hamcrest import *
from matchers import has_len

assert_that((i for i in [1, 2, 3]), has_len(3))

has_keys

from hamcrest import *
from matchers import has_keys

dictionary = {
    'first': 'foo',
    'second': 'bar'
}

assert_that(dictionary, has_keys(['first', 'second']))

matches_re

from hamcrest import *
from matchers import matches_re

assert_that('pattern', matches_re(r'pattern'))

callable_

from hamcrest import *
from matchers import callable_

assert_that(lambda : 'foo', is_(callable_()))

json_

from hamcrest import *
from matchers import json_

assert_that("{'foo': ['bar']}", is_(json_()))
assert_that("{'foo': ['bar']}", is_(json_(has_key('foo'))))

subset_of

from hamcrest import *
from matchers import subset_of

assert_that([1, 2], is_(subset_of([1, 2, 3])))

superset_of

from hamcrest import *
from matchers import superset_of

assert_that([1, 2, 3], is_(superset_of([1, 2])))

disjoint_with

from hamcrest import *
from matchers import disjoint_with

assert_that([1, 2, 3], is_(disjoint_with([4, 5, 6])))

xml_document

from hamcrest import *
from matchers import xml_document
from xml.etree import Element

assert_that('<element/>', is_(xml_document()))
assert_that('<element/>', is_(xml_document(instance_of(Element))))

xml_root

from hamcrest import *
from matchers import xml_root

assert_that('<element/>', xml_root(tag='element'))

xml_element

from hamcrest import *
from matchers import xml_document, xml_element

assert_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_element

from hamcrest import *
from matchers import xml_root, xml_element, xml_contains_element

assert_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_namespaced

from hamcrest import *
from matchers import xml_namespaced

assert_that('<element xmlns="http://foo.com"/>',
    is_(xml_namespaced('http://foo.com')))

soap_document

from hamcrest import *
from matchers import xml_document, soap_document

ns_url = "http://schemas.xmlsoap.org/soap/envelope/"
string = "<Envelope xmlns='" + ns_url + "' />"

assert_that(string, is_(xml_document(is_(soap_document()))))

soap_message

from hamcrest import *
from matchers import xml_document, soap_document, soap_message

ns_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_displayed

from hamcrest import *
from matchers import is_displayed

from selenium import webdriver

browser = webdriver.Firefox()
browser.open('wwww.google.com')

logo = browser.find_element_by_css_selector('#hplogo')

assert_that(logo, is_displayed())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matchers-0.22.tar.gz (7.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page