import sure

(4) + 2)
(7.5).should.eql(3.5 + 4)
(2).should.equal(8 / 4)


(float).should.equal(float, epsilon)

import sure

(4.242423), epsilon=0.000005)
(4.01), epsilon=0.01)
(6.3699999).should.equal(6.37, epsilon=0.001)

(4.242423), epsilon=0.000005)

Compare strings with diff


import sure

XML1 = '''<root>
  <a-tag with-attribute="one">AND A VALUE</a-tag>

XML2 = '''<root>
  <a-tag with-attribute="two">AND A VALUE</a-tag>

this will give you and output like


-   <a-tag with-attribute="one">AND A VALUE</a-tag>
?                           --
+   <a-tag with-attribute="two">AND A VALUE</a-tag>
?                          ++

{'a': 'collection'}.should.equal({'a': 'collection'}) does deep comparison

{'foo': 'bar'}.should.equal({'foo': 'bar'})
{'foo': 'bar'}.should.eql({'foo': 'bar'})
{'foo': 'bar'}{'foo': 'bar'})

“A string”.lower().should.equal(“a string”) also works

"Awesome ASSERTIONS".lower().split().should.equal(['awesome', 'assertions'])


should.look_like and should_not.look_like


THIS IS MY loose string
""".should.look_like('this is my loose string')

"""this one is different""".should_not.look_like('this is my loose string')


should.contain and should_not.contain

expect(collection).to.contain(item) is a shorthand to expect(item)

"My bucket of text".should.contain('bucket')



should.match and should_not.match matches regular expression

You can also use the modifiers:

import re

"SOME STRING".should.match(r'some \w+', re.I)

"FOO BAR CHUCK NORRIS".should_not.match(r'some \w+', re.M)

{iterable} applies to any iterable of length 0


## negate with:

[1, 2, 3];
"Lincoln de Sousa";
"Lincoln de Sousa";

{number}, 10) asserts inclusive numeric range:

(1), 2)

## negate with:

(1), 6)

{member}{iterable}) asserts that a member is part of the iterable:

'name'{'name': 'Gabriel'})
'Lincoln'['Lincoln', 'Gabriel'])

## negate with:

'Bug'['Sure 1.0'])
'Bug'['Sure 1.0']) and

Assert whether an object is or not None:

value = None

(not None) and

Assert truthfulness:

from sure import this
'truthy string'
{'truthy': 'dictionary'}

And negate truthfulness:

from sure import this

Assert existence of properties and their values

class Basket(object):
    fruits = ["apple", "banana"]

basket1 = Basket()"fruits") allows chaining up

If the programmer calls it returns an assertion builder of the property if it exists, so that you can chain up assertions for the property value itself.

class Basket(object):
    fruits = ["apple", "banana"]

basket2 = Basket()"fruits").being.equal(["apple", "banana"])"fruits").with_value.equal(["apple", "banana"])"fruits").with_value.being.equal(["apple", "banana"])

Assert existence of keys and its values

basket3 = dict(fruits=["apple", "banana"])

.have.key().being allows chaining up

If the programmer calls have.key() it returns an assertion builder of the key if it exists, so that you can chain up assertions for the dictionary key value itself.

person = dict(name=None)


Assert the length of objects with {iterable}.should.have.length_of(N)

[3, 4].should.have.length_of(2)


{'john': 'person'}.should_not.have.length_of(2)

Assert the magnitude of objects with {X} and {Y} as well as {X} and {Y}



callable.when.called_with(arg1, kwarg1=2).should.throw(Exception)

You can use this feature to assert that a callable raises an exception:

import sure
from six import PY3

if PY3:
    range.when.called_with(10, step=20).should.throw(TypeError, "range() does not take keyword arguments")
    range.when.called_with("chuck norris").should.throw(TypeError, "'str' object cannot be interpreted as an integer")
    range.when.called_with(10, step="20").should.throw(TypeError, "range() takes no keyword arguments")
    range.when.called_with(b"chuck norris").should.throw("range() integer end argument expected, got str.")
range.when.called_with("chuck norris").should.throw(TypeError)

You can also match regular expressions with to the expected exception messages:

import re
range.when.called_with(10, step=20).should.throw(TypeError, re.compile(r'(does not take|takes no) keyword arguments'))
range.when.called_with("chuck norris").should.throw(TypeError, re.compile(r'(cannot be interpreted as an integer|integer end argument expected)'))

function.when.called_with(arg1, kwarg1=2).should.return_value(value)

This is a shorthand for testing that a callable returns the expected result

import sure

list.when.called_with([0, 1]).should.return_value([0, 1])

this is the same as

value = range(2)
value.should.equal([0, 1])

there are no differences between those 2 possibilities, use at will'typename') and'typename')

this takes a type name and checks if the class matches that name

import sure


## also works with paths to modules

range(10)'collections.Iterable') and

this takes the class (type) itself and checks if the object is an instance of it

import sure
from six import PY3

if PY3:
[] and

assert the instance value above and below num

import sure


Static assertions with it, this, those and these

Whether you don't like the object.should syntax or you are simply not running CPython, sure still allows you to use any of the assertions above, all you need to do is wrap the object that is being compared in one of the following options: it, this, those and these.

Too long, don't read

All those possibilities below work just as the same

from sure import it, this, those, these

(10) + 5)

this(10) + 5)

it(10) + 5)

these(10) + 5)

those(10) + 5)

Also if you prefer using the assert keyword in your tests just go ahead an do it!

from sure import it, this, those, these, expect

assert (10) + 5)

assert this(10) + 5)

assert it(10) + 5)

assert these(10) + 5)

assert those(10) + 5)

expect(10) + 5)

(lambda: None)

Test if something is or not callable

import sure
(lambda: None);

A note about the assert keyword

you can use or not the assert keyword, sure internally already raises an appropriate AssertionError with an assertion message so that you don't have to specify your own, but you can still use assert if you find it more semantic


import sure


## or you can also use

assert "Name".lower().should.equal('name')

## or still

from sure import this

assert this("Name".lower()).should.equal('name')

## also without the assert


Any of the examples above will raise their own AssertionError with a meaningful error message.


Sure provides you with a lot of synonyms so that you can pick the ones that makes more sense for your tests.

Note that the examples below are merely illustrative, they work not only with numbers but with any of the assertions you read early in this documentation.

Positive synonyms

(2 + 2)
(2 + 2)
(2 + 2).does.equals(4)
(2 + 2).do.equals(4)

Negative synonyms

from sure import expect



Chain-up synonyms

Any of those synonyms work as an alias to the assertion builder:

  • be
  • being
  • to
  • when
  • have
  • with_value
from sure import expect

{"foo": 1}.must.with_value.being.equal({"foo": 1})
{"foo": 1}.does.have.key("foo").being.with_value.equal(1)

Equality synonyms


Positive boolean synonyms

(not None)
(not None)
(not None)

Negative boolean synonyms

Holy guacamole, how did you implement that feature ?

Differently of ruby python doesn't have open classes, but Lincoln de Sousa came out with a super sick code that uses the ctypes module to create a pointer to the __dict__ of builtin types.

Yes, it is dangerous, non-pythonic and should not be used in production code.

Although sure is here to be used ONLY in test code, therefore it should be running in ONLY possible environments: your local machine or your continuous-integration server.

About sure 1.0

The assertion library is 100% inspired be the awesomeness of should.js which is simple, declarative and fluent.

Sure strives to provide everything a python developer needs in an assertion:

  • Assertion messages are easy to understand

  • When comparing iterables the comparation is recursive and shows exactly where is the error

  • Fluency: the builtin types are changed in order to provide awesome simple assertions