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

Installation

python setup.py install

Dependences

  • lxml

  • 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 lxml.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()))))))

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.20.tar.gz (6.8 kB view details)

Uploaded Source

File details

Details for the file matchers-0.20.tar.gz.

File metadata

  • Download URL: matchers-0.20.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for matchers-0.20.tar.gz
Algorithm Hash digest
SHA256 d4b86a6fc4dfb1ae32292f34fb1a43391ce2971b88ccb9698af6c7b3a599824e
MD5 bd22a5882b8cb1f6def9e32d11662d94
BLAKE2b-256 c4f715d9f139d703c73d28d5a45d3b10aa2847ef1af19154e2e9173ea25d4912

See more details on using hashes here.

Supported by

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