Matchers

equal

expect(1).to(equal(1))

expect(1).not_to(equal(2))

expect(1).to(equal(2))

Failure

Expected 1 to equal 2

expect(1).not_to(equal(1))

Failure

Expected 1 not to equal 1

be

class Foo(object):
    pass

value = Foo()

expect(value).to(be(value))

expect(1).not_to(be(2))

expect(1).to(be(2))

Failure

Expected 1 to be 2

expect(value).not_to(be(value))

Failure

Expected <Foo object at 0x7ff289cb4310> not to be <Foo object at 0x7ff289cb4310>

be_true

expect(True).to(be_true)

expect(False).not_to(be_true)

expect(False).to(be_true)

Failure

Expected False to be True

expect(True).not_to(be_true)

Failure

Expected True not to be True

be_false

expect(False).to(be_false)

expect(True).not_to(be_false)

expect(True).to(be_false)

Failure

Expected True to be False

expect(False).not_to(be_false)

Failure

Expected False not to be False

be_none

expect(None).to(be_none)

expect('foo').not_to(be_none)

expect(True).to(be_none)

Failure

Expected True to be None

expect(None).not_to(be_none)

Failure

Expected None not to be None

be_a / be_an

class Foo(object):
    pass

class Bar(object):
    pass

class Object(object):
    pass

expect(Foo()).to(be_a(Foo))

expect(Foo()).not_to(be_a(Bar))

expect(Foo()).to(be_an(object))

expect(Foo()).not_to(be_an(Object))

expect(Foo()).to(be_a(Bar))

Failure

Expected <Foo object at 0x7ff289cb4310> to be a Bar

expect(Foo()).to_not(be_a(Foo))

Failure

Expected <Foo object at 0x7ff289cb4310> not to be a Foo

expect(Foo()).to(be_an(Object))

Failure

Expected <Foo object at 0x7ff289cb4310> to be an Object

expect(Foo()).not_to(be_an(object))

Failure

Expected <Foo object at 0x7ff289cb4310> not to be an object

be_empty

expect('').to(be_empty)

expect(iter('')).to(be_empty)

expect('foo').not_to(be_empty)

expect('foo').to(be_empty)

Failure

Expected 'foo' to be empty

expect(iter('foo')).to(be_empty)

Failure

Expected <str_iterator object at 0x7fd4832d6950> to be empty

expect('').to_not(be_empty)

Failure

Expected '' not to be empty

be_above

expect(5).to(be_above(4))

expect(1).not_to(be_above(4))

expect(1).to(be_above(4))

Failure

Expected 1 to be above 4

expect(5).not_to(be_above(4))

Failure

Expected 5 not to be above 4

be_below

expect(1).to(be_below(4))

expect(4).not_to(be_below(1))

expect(4).to(be_below(1))

Failure

Expected 4 to be below 1

expect(1).not_to(be_below(4))

Failure

Expected 1 not to be below 4

be_above_or_equal

expect(5).to(be_above_or_equal(4))

expect(5).to(be_above_or_equal(5))

expect(1).to_not(be_above_or_equal(4))

expect(1).to(be_above_or_equal(4))

Failure

Expected 1 to be above or equal 4

expect(5).not_to(be_above_or_equal(4))

Failure

Expected 5 not to be above or equal 4

expect(5).not_to(be_above_or_equal(5))

Failure

Expected 5 not to be above or equal 5

be_below_or_equal

expect(1).to(be_below_or_equal(4))

expect(5).to(be_below_or_equal(5))

expect(4).not_to(be_below_or_equal(1))

expect(4).to(be_below_or_equal(1))

Failure

Expected 4 to be below or equal 1

expect(1).not_to(be_below_or_equal(4))

Failure

Expected 1 not to be below or equal 4

expect(5).not_to(be_below_or_equal(5))

Failure

Expected 5 not to be below or equal 5

be_within

expect(5).to(be_within(4, 7))

expect(5.5).to(be_within(4, 7))

expect(1).not_to(be_within(4, 7))

expect(1).to(be_within(4, 7))

Failure

Expected 1 to be within 4, 7

expect(5).not_to(be_within(4, 7))

Failure

Expected 5 not to be within 4, 7

be_callable

expect(lambda: None).to(be_callable)

expect('foo').to(be_callable)

Failure

Expected 'foo' to be callable

have_len / have_length

expect('foo').to(have_len(3))

expect('foo').to(have_len(be_above_or_equal(3)))

expect(iter('foo')).to(have_length(3))

expect('foo').not_to(have_len(2))

expect('foo').to(have_length(2))

Failure

Expected 'foo' to have length 2

expect('foo').to(have_len(be_bellow(2)))

Failure

Expected 'foo' to have len be bellow 2

expect(iter('foo')).to(have_len(2))

Failure

Expected <str_iterator object at 0x7fd4832d6950> to have len 2

expect('foo').not_to(have_len(3))

Failure

Expected 'foo' not to have len 3

have_property

class Foo(object):
    def __init__(self, **kwargs):
        for name, value in kwargs.items():
            setattr(self, name, value)

expect(Foo(bar=0, baz=1)).to(have_property('bar'))

expect(Foo(bar=0, baz=1)).to(have_property('bar', 0))

expect(Foo(bar=0, baz=1)).not_to(have_property('foo'))

expect(Foo(bar=0, baz=1)).not_to(have_property('foo', 0))

expect(Foo(bar=0, baz=1)).not_to(have_property('bar', 1))

expect(Foo(bar=0, baz=1)).to(have_property('bar', be_below(1)))

expect(Foo(bar=0, baz=1)).to(have_property('bar', not_(be_above(1))))

expect(Foo(bar=0, baz=1)).to(have_property('foo'))

Failure

Expected <Foo object at 0x7ff289cb4310> to have property 'foo'

expect(Foo(bar=0, baz=1)).to(have_property('foo', 0))

Failure

Expected <Foo object at 0x7ff289cb4310> to have property 'foo' equal 0

expect(Foo(bar=0, baz=1)).to(have_property('bar', 1))

Failure

Expected <Foo object at 0x7ff289cb4310> to have property 'bar' equal 1

expect(Foo(bar=0, baz=1)).to(have_property('bar', None))

Failure

Expected <Foo object at 0x7ff289cb4310> to have property 'bar' equal None

expect(Foo(bar=0, baz=1)).not_to(have_property('bar'))

Failure

Expected <Foo object at 0x7ff289cb4310> not to have property 'bar'

expect(Foo(bar=0, baz=1)).not_to(have_property('bar', 0))

Failure

Expected <Foo object at 0x7ff289cb4310> not to have property 'bar' equal 0

expect(Foo(bar=0, baz=1)).to(have_property('bar', be_above(1)))

Failure

Expected <Foo object at 0x7ff289cb4310> to have property 'bar' be above 1

expect(Foo(bar=0, baz=1)).to(have_property('bar', not_(be_below(1))))

Failure

Expected <Foo object at 0x7ff289cb4310> to have property 'bar' not be below 1

have_properties

class Foo(object):
    def __init__(self, **kwargs):
        for name, value in kwargs.items():
            setattr(self, name, value)

expect(Foo(bar=0, baz=1)).to(have_properties('bar', 'baz'))

expect(Foo(bar=0, baz=1)).to(have_properties(bar=0, baz=1))

expect(Foo(bar=0, baz=1)).to(have_properties('bar', baz=1))

expect(Foo(bar=0, baz=1)).to(have_properties({'bar': 0, 'baz': 1}))

expect(Foo(bar=0, baz=1)).to(have_properties(bar=be_an(int)))

expect(Foo(bar=0, baz=1)).to_not(have_properties('foo', 'foobar'))

expect(Foo(bar=0, baz=1)).to_not(have_properties(foo=0, foobar=1))

expect(Foo(bar=0, baz=1)).not_to(have_properties(foo=0, bar=1))

expect(Foo(bar=0, baz=1)).not_to(have_properties({'foo': 0, 'foobar': 1}))

expect(Foo(bar=0, baz=1)).not_to(have_properties({'foo': 0, 'bar': 1}))

expect(Foo(bar=0, baz=1)).not_to(have_properties('foo', 'bar'))

expect(Foo(bar=0, baz=1)).to(have_properties('bar', 'foo'))

Failure

Expected <Foo object at 0x7ff289cb4310> to have properties 'bar' and 'foo'

expect(Foo(bar=0, baz=1)).to(have_properties(bar=0, foo=1))

Failure

Expected <Foo object at 0x7ff289cb4310> to have properties 'bar' equal 0 and 'foo' equal 1

expect(Foo(bar=0, baz=1)).to(have_properties(bar=1, baz=1))

Failure

Expected <Foo object at 0x7ff289cb4310> to have properties 'bar' equal 1 and 'baz' equal 1

expect(Foo(bar=0, baz=1)).to(have_properties('foo', bar=0))

Failure

Expected <Foo object at 0x7ff289cb4310> to have properties 'foo' and 'bar' equal 0

expect(Foo(bar=0, baz=1)).to(have_properties('baz', bar=1))

Failure

Expected <Foo object at 0x7ff289cb4310> to have properties 'baz' and 'bar' equal 1

expect(Foo(bar=0, baz=1)).to(have_properties({'bar': 1, 'baz': 1}))

Failure

Expected <Foo object at 0x7ff289cb4310> to have properties 'bar' equal 1 and 'baz' equal 1

expect(Foo(bar=0, baz=1)).not_to(have_properties('bar', 'baz'))

Failure

Expected <Foo object at 0x7ff289cb4310> not to have properties 'bar' and 'baz'

expect(Foo(bar=0, baz=1)).not_to(have_properties(bar=0, baz=1))

Failure

Expected <Foo object at 0x7ff289cb4310> not to have properties 'bar' equal 0 and 'baz' equal 1

expect(Foo(bar=0, baz=1)).to(have_properties(bar=be_a(str)))

Failure

Expected <Foo object at 0x7ff289cb4310> not to have properties 'bar' be a str

have_key

expect({'bar': 0, 'baz': 1}).to(have_key('bar'))

expect({'bar': 0, 'baz': 1}).to(have_key('bar', 0))

expect({'bar': 0, 'baz': 1}).not_to(have_key('foo'))

expect({'bar': 0, 'baz': 1}).not_to(have_key('foo', 0))

expect({'bar': 0, 'baz': 1}).to_not(have_key('bar', 1))

expect({'bar': 0, 'baz': 1}).to(have_key('bar', be_below(1)))

expect({'bar': 0, 'baz': 1}).to(have_key('foo'))

Failure

Expected {'bar': 0, 'baz': 1} to have key 'foo'

expect({'bar': 0, 'baz': 1}).to(have_key('foo', 0))

Failure

Expected {'bar': 0, 'baz': 1} to have key 'foo' equal 0

expect({'bar': 0, 'baz': 1}).to(have_key('bar', 1))

Failure

Expected {'bar': 0, 'baz': 1} to have key 'bar' equal 1

expect({'bar': 0, 'baz': 1}).to(have_key('bar', None))

Failure

Expected {'bar': 0, 'baz': 1} to have key 'bar' equal None

expect('My foo string').to(have_key('foo', 0))

Failure

Expected {'bar': 0, 'baz': 1} to have key 'foo' equal 0 but is not a dict

expect({'bar': 0, 'baz': 1}).not_to(have_key('bar'))

Failure

Expected {'bar': 0, 'baz': 1} not to have key 'bar'

expect({'bar': 0, 'baz': 1}).not_to(have_key('bar', 0))

Failure

Expected {'bar': 0, 'baz': 1} not to have key 'bar' equal 0

expect('My foo string').not_to(have_key('foo', 0))

Failure

Expected {'bar': 0, 'baz': 1} not to have key 'foo' equal 0 but is not a dict

expect({'bar': 0, 'baz': 1}).to(have_key('bar', be_above(1)))

Failure

Expected {'bar': 0, 'baz': 1} to have key 'bar' be above 1

have_keys

expect({'bar': 0, 'baz': 1}).to(have_keys('bar', 'baz'))

expect({'bar': 0, 'baz': 1}).to(have_keys(bar=0, baz=1))

expect({'bar': 0, 'baz': 1}).to(have_keys('bar', baz=1))

expect({'bar': 0, 'baz': 1}).to(have_keys({'bar': 0, 'baz': 1}))

expect({'bar': 0, 'baz': 1}).not_to(have_keys('foo', 'foobar'))

expect({'bar': 0, 'baz': 1}).not_to(have_keys(foo=0, foobar=1))

expect({'bar': 0, 'baz': 1}).not_to(have_keys(foo=0, bar=1))

expect({'bar': 0, 'baz': 1}).not_to(have_keys({'foo': 0, 'foobar': 1}))

expect({'bar': 0, 'baz': 1}).not_to(have_keys('foo', 'bar'))

expect({'bar': 0, 'baz': 1}).to(have_keys('bar', 'foo'))

Failure

Expected {'bar': 0, 'baz': 1} to have keys 'bar' and 'foo'

expect({'bar': 0, 'baz': 1}).to(have_keys(bar=0, foo=1))

Failure

Expected {'bar': 0, 'baz': 1} to have keys 'bar' equal 0 and 'foo' equal 1

expect({'bar': 0, 'baz': 1}).to(have_keys(bar=1, baz=1))

Failure

Expected {'bar': 0, 'baz': 1} to have keys 'bar' equal 1 and 'baz' equal 1

expect({'bar': 0, 'baz': 1}).to(have_keys('foo', 'fuu', bar=0))

Failure

Expected {'bar': 0, 'baz': 1} to have keys 'foo', 'fuu' and 'bar' equal 0

expect({'bar': 0, 'baz': 1}).to(have_keys('baz', bar=1))

Failure

Expected {'bar': 0, 'baz': 1} to have keys 'baz' and 'bar' equal 1

expect({'bar': 0, 'baz': 1}).to(have_keys({'bar': 1, 'baz': 1}))

Failure

Expected {'bar': 0, 'baz': 1} to have keys 'bar' equal 1 and 'baz' equal 1

expect('My foo string').to(have_keys({'bar': 1, 'baz': 1}))

Failure

Expected {'bar': 0, 'baz': 1} to have keys 'bar' equal 1 and 'baz' equal 1 but is not a dict

expect({'bar': 0, 'baz': 1}).not_to(have_keys('bar', 'baz'))

Failure

Expected {'bar': 0, 'baz': 1} not to have keys 'bar' and 'baz'

expect({'bar': 0, 'baz': 1}).not_to(have_keys(bar=0, baz=1))

Failure

Expected {'bar': 0, 'baz': 1} not to have keys 'bar' equal 0 and 'baz' equal 1

expect('My foo string').not_to(have_keys({'bar': 1, 'baz': 1}))

Failure

Expected {'bar': 0, 'baz': 1} not to have keys 'bar' equal 1 and 'baz' equal 1 but is not a dict

contain

expect(['bar', 'baz']).to(contain('bar'))

expect(['bar', 'baz']).to(contain('bar', 'baz'))

expect(['bar', 'baz']).to(contain('baz', 'bar'))

expect([{'foo': 1}, 'bar']).to(contain({'foo': 1}))

expect(iter(['bar', 'baz'])).to(contain('bar'))

expect(iter(['bar', 'baz'])).to(contain('bar', 'baz'))

expect('My foo string').to(contain('foo'))

expect('My foo string').to(contain('foo', 'string'))

expect(['bar', 'baz']).not_to(contain('foo'))

expect(['bar', 'baz']).not_to(contain('foo', 'foobar'))

expect(['bar', 'baz']).not_to(contain('bar', 'foo'))

expect(['bar', 'baz']).to(contain(be_a(str)))

expect(['bar', 'baz']).to(contain('bar', 'foo'))

Failure

Expected ['bar', 'baz'] to contain 'bar' and 'foo'

expect(iter(['bar', 'baz'])).to(contain('bar', 'foo'))

Failure

Expected ['bar', 'baz'] to contain 'bar' and 'foo'

expect(object()).to(contain('bar'))

Failure

Expected <object object at 0x7f5004aa1070> to contain 'bar' but is not a valid sequence type

expect(['bar', 'baz']).not_to(contain('bar'))

Failure

Expected ['bar', 'baz'] not to contain 'bar'

expect(['bar', 'baz']).not_to(contain('bar', 'baz'))

Failure

Expected ['bar', 'baz'] not to contain 'bar' and 'baz'

expect(object()).not_to(contain('bar'))

Failure

Expected <object object at 0x7f5004aa1070> not to contain 'bar' but is not a valid sequence type

expect(['bar', 'baz']).to(contain(be_an(int), have_len(5)))

Failure

Expected ['bar', 'baz'] to contain be an int and have len 5

contain_exactly

expect(['bar']).to(contain_exactly('bar'))

expect(['bar', 'baz']).to(contain_exactly('bar', 'baz'))

expect('My foo string').to(contain_exactly('My foo string'))

expect('My foo string').to(contain_exactly('My foo', ' string'))

expect(['bar', 'baz']).to(contain_exactly(equal('bar'), equal('baz')))

expect(['bar', 'baz']).to(contain_exactly('foo'))

Failure

Expected ['bar', 'baz'] to contain exactly 'foo'

expect(['bar', 'baz']).to(contain_exactly('foo', 'fuu'))

Failure

Expected ['bar', 'baz'] to contain exactly 'foo' and 'fuu'

expect(['bar', 'baz']).to(contain_exactly('baz', 'bar'))

Failure

Expected ['bar', 'baz'] to contain exactly 'baz' and 'bar'

expect(['bar', 'baz']).to(contain_exactly('bar'))

Failure

Expected ['bar', 'baz'] to contain exactly 'bar'

expect(['bar', 'baz', 'foo']).to(contain_exactly('bar', 'baz'))

Failure

Expected ['bar', 'baz', 'foo'] to contain exactly 'bar' and 'baz'

expect(['bar', 'baz', 'foo', 'fuu']).to(contain_exactly('bar', 'baz', 'foo'))

Failure

Expected ['bar', 'baz', 'foo', 'fuu'] to contain exactly 'bar', 'baz' and 'foo'

expect('My foo string').to(contain_exactly('foo'))

Failure

Expected 'My foo string' to contain exactly 'foo'

expect(object()).to(contain_exactly('bar'))

Failure

Expected <object object at 0x7f5004aa1070> to contain exactly 'bar' but is not a valid sequence type

expect(['bar', 'baz']).to(contain_exactly(equal('baz'), equal('baz')))

Failure

Expected ['bar', 'baz'] to contain exactly equal 'bar' and equal 'baz'

contain_only

expect(['bar']).to(contain_only('bar'))

expect(['bar', 'baz']).to(contain_only(['baz', 'bar']))

expect(iter(['bar', 'baz'])).to(contain_only('bar', 'baz'))

expect('My foo string').to(contain_only('My foo string'))

expect('My foo string').to(contain_only('My foo', ' string'))

expect(['bar', 'baz']).to(contain_only(equal('bar'), equal('baz')))

expect(['bar', 'baz']).to(contain_only('foo'))

Failure

Expected ['bar', 'baz'] to contain only 'foo'

expect(['bar', 'baz', 'foo']).to(contain_only('bar', 'baz'))

Failure

Expected ['bar', 'baz', 'foo'] to contain only 'bar' and 'baz'

expect('My foo string').to(contain_only('foo'))

Failure

Expected 'My foo string' to contain only 'foo'

expect(object()).to(contain_only('bar'))

Failure

Expected <object object at 0x7f5004aa1070> to contain only 'bar' but is not a valid sequence type

expect(['bar', 'baz']).to(contain_only(equal('baz'), equal('foo')))

Failure

Expected ['bar', 'baz'] to contain only equal 'baz' and equal 'foo'

start_with

expect('My foo string').to(start_with('My foo'))

expect('My foo string').not_to(start_with('tring'))

expect([1, 2, 3]).to(start_with(1))

expect([1, 2, 3]).to(start_with(1, 2))

expect(OrderedDict([('bar', 0), ('baz', 1)])).to(start_with('bar', 'baz'))

expect(iter([1, 2, 3])).to(start_with(1, 2))

expect([1, 2, 3]).not_to(start_with(2, 3))

expect([1, 2, 3]).not_to(start_with(1, 1))

expect('My foo string').to(start_with('tring'))

Failure

Expected 'My foo string' to start with 'tring'

expect([1, 2, 3]).to(start_with(2))

Failure

Expected [1, 2, 3] to start with 2

expect([1, 2, 3]).to(start_with(2, 3))

Failure

Expected [1, 2, 3] to start with 2 and 3

expect([1, 2, 3]).to(start_with(1, 1))

Failure

Expected [1, 2, 3] to start with 1 and 1

expect({'bar': 0, 'baz': 1}).to(start_with('bar', 'baz'))

Failure

Expected {'bar': 0, 'baz': 1} to start with 'bar' and 'baz' but it does not have ordered keys

end_with

expect('My foo string').to(end_with('tring'))

expect('My foo string').not_to(end_with('My foo'))

expect([1, 2, 3]).to(end_with(3))

expect([1, 2, 3]).to(end_with(2, 3))

expect(OrderedDict([('bar', 0), ('baz', 1)])).to(end_with('bar, 'baz'))

expect([1, 2, 3]).to_not(end_with(1, 2))

expect([1, 2, 3]).to_not(end_with(3, 3))

expect('My foo string').to(end_with('My fo'))

Failure

Expected 'My foo string' to end with 'My fo'

expect([1, 2, 3]).to(end_with(3, 3))

Failure

Expected [1, 2, 3] to end with 3 and 3

expect({'bar': 0, 'baz': 1}).to(end_with('baz', 'bar'))

Failure

Expected {'bar': 0, 'baz': 1} to end with 'baz' and 'bar' but it does not have ordered keys

match

expect('My foo string').to(match(r'My \w+ string'))

expect('My foo string').to(match(r'\w+ string'))

expect('My foo string').to(match(r'my [A-Z]+ strinG', re.I))

expect('My foo string').not_to(match(r'My \W+ string'))

expect('My foo string').not_to(match(r'My \W+ string', re.I))

expect('My foo string').to(match(pattern))

Failure

Expected 'My foo string' to match r'My \\W+ string'

expect('My foo string').not_to(match(r'My \w+ string'))

Failure

Expected 'My foo string' not to match r'My \\w+ string'

raise_error

def callback():
    raise AttributeError('error message')

expect(callback).to(raise_error)

expect(callback).to(raise_error(AttributeError))

expect(callback).to(raise_error(AttributeError, 'error message'))

expect(callback).to(raise_error(AttributeError, match(r'error \w+')))

def callback():
    raise AttributeError(2)

expect(callback).to(raise_error(AttributeError, 2))

def callback():
    raise KeyError()

expect(callback).to(raise_error(AttributeError))

Failure

Expected <function callback at 0x7fe70cb103b0> to raise AttributeError but KeyError raised

expect(lambda: None).to(raise_error(AttributeError))

Failure

Expected <function <lambda> at 0x7f3e670863b0> to raise AttributeError but not raised

def callback():
    raise AttributeError('bar')

expect(callback).to(raise_error(AttributeError, 'foo'))

Failure

Expected <function callback at 0x7fe70cb103b0> to raise AttributeError with message 'foo' but message was 'bar'