API Reference


You might be familiar with to how the unittest module suggests to implement setup and teardown callbacks for your tests.

But if you prefer to define test cases as functions and use a runner like nose then sure can help you define and activate modular fixtures.

In sure’s parlance, we call it a Scenario

Example: Setup a Flask app for testing


import json
from flask import Response, Flask

webapp = Flask(__name__)

def index():
    data = json.dumps({'hello': 'world'}}
    return Response(data, headers={'Content-Type': 'application/json'})


from sure import scenario
from my_flask_app import webapp

def prepare_webapp(context):
    context.server = webapp.test_client()

web_scenario = scenario(prepare_webapp)


import json
from sure import scenario
from tests.scenarios import web_scenario

def test_hello_world(context):
    # Given that I GET /
    response = context.server.get('/')

    # Then it should have returned a successful json response

    json.loads(response.data).should.equal({'hello': 'world'})

Example: Multiple Setup and Teardown functions


import os
import shutil
from sure import scenario

def prepare_directories(context):
    context.root = os.path.dirname(os.path.abspath(__file__))
    context.fixture_path = os.path.join(context.root, 'input_data')
    context.result_path = os.path.join(context.root, 'output_data')
    context.directories = [

    for path in context.directories:
        if os.path.isdir(path):


def cleanup_directories(context):
    for path in context.directories:
        if os.path.isdir(path):

def create_10_dummy_hex_files(context):
    for index in range(10):
        filename = os.path.join(context.fixture_path, 'dummy-{}.hex'.format(index))
        open(filename, 'wb').write(os.urandom(32).encode('hex'))

dummy_files_scenario = scenario([create_directories, create_10_dummy_hex_files], [cleanup_directories])


import os
from tests.scenarios import dummy_files_scenario

def test_files_exist(context):

Number Equality

(2 + 2).should.equal(4)

import sure

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


.equal(float, epsilon)

import sure

(4.242423).should.be.equal(4.242420, epsilon=0.000005)
(4.01).should.be.eql(4.00, epsilon=0.01)
(6.3699999).should.equal(6.37, epsilon=0.001)

(4.242423).shouldnt.be.equal(4.249000, epsilon=0.000005)

String Equality


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>
?                          ++

.should.equal("a string")

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

String Similarity



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')

Strings Matching Regular-Expressions


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)

Collections and Iterables

Works with:

  • Lists, Tuples, Sets

  • Dicts, OrderedDicts

  • Anything that implements __iter__() / next()

.equal({'a': 'collection'})

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


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

['1.2.5', '1.2.4'].should.contain('1.2.5')
'1.2.4'].should.be.within(['1.2.5', '1.2.4'])

# also works with strings
"My bucket of text".should.contain('bucket')



## negate with:

[1, 2, 3].shouldnt.be.empty
"Dummy String".shouldnt.be.empty
"Dummy String".should_not.be.empty

{number}.should.be.within(0, 10)

asserts inclusive numeric range

(1).should.be.within(0, 2)
(5).should.be.within(0, 10)

## negate with:

(1).shouldnt.be.within(5, 6)


asserts that a member is part of the iterable

'name'.should.be.within({'name': 'Gabriel'})
'Lincoln'.should.be.within(['Lincoln', 'Gabriel'])

## negate with:

'Bug'.shouldnt.be.within(['Sure 1.0'])
'Bug'.should_not.be.within(['Sure 1.0'])


Assert whether an object is or not None

value = None

(not None).should_not.be.none


Assert truthfulness:

from sure import this

'truthy string'.should.be.ok
{'truthy': 'dictionary'}.should.be.ok

And negate truthfulness:

from sure import this



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

basket1 = Basket()


If the programmer calls have.property() 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()
basket2.should.have.property("fruits").which.should.be.equal(["apple", "banana"])
basket2.should.have.property("fruits").being.equal(["apple", "banana"])
basket2.should.have.property("fruits").with_value.equal(["apple", "banana"])
basket2.should.have.property("fruits").with_value.being.equal(["apple", "banana"])


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

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

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


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

{X}.should.be.greater_than(Y) and {Y}.should.be.lower_than(X)

Assert the magnitude of objects with {X}.should.be.greater_than(Y) and {Y}.should.be.lower_than(X) as well as {X}.should.be.greater_than_or_equal_to(Y) and {Y}.should.be.lower_than_or_equal_to(X).




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

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

range.when.called_with("chuck norris").should.have.raised(TypeError)

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

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


An idiomatic alias to .should.have.raised.

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.")

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

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

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

which equates to:

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

there are no differences between those 2 possibilities, use at will


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

import sure


## also works with paths to modules



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

import sure
from six import PY3

if PY3:

.be.above(num) and .be.below(num)

assert the instance value above and below num

import sure


it(), this(), those(), these()

.should aliases to make your tests more idiomatic.

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

from sure import it, this, those, these

(10).should.be.equal(5 + 5)

this(10).should.be.equal(5 + 5)
it(10).should.be.equal(5 + 5)
these(10).should.be.equal(5 + 5)
those(10).should.be.equal(5 + 5)

Every assertion returns True when succeeded, and if failed the AssertionError is already raised internally by sure, with a nice description of what failed to match, too.

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

assert (10).should.be.equal(5 + 5)
assert this(10).should.be.equal(5 + 5)
assert it(10).should.be.equal(5 + 5)
assert these(10).should.be.equal(5 + 5)
assert those(10).should.be.equal(5 + 5)

expect(10).to.be.equal(5 + 5)

(lambda: None).should.be.callable

Test if something is or not callable

import sure

(lambda: None).should.be.callable


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).should.be.equal(4)
(2 + 2).must.be.equal(4)
(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

import sure
(not None).should.be.ok
(not None).should.be.truthy
(not None).should.be.true

Negative boolean synonyms

import sure

Differently of ruby python doesn’t have open classes, but sure uses a technique involving the module ctypes to write directly in the private __dict__ of in-memory objects. For more information check out the Forbidden Fruit project.

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.

API Builtin Documentation

class sure.VariablesBag(*args, **kw)[source]

Extend sure with a custom assertion method.


Extend sure with a custom chaining method.


Extend sure with a custom chain property.

class sure.ensure(msg, *args, **kwargs)[source]

Contextmanager to ensure that the given assertion message is printed upon a raised AssertionError exception.

The args and kwargs are used to format the message using format().

class sure.core.Anything[source]

Represents any possible value. Its existence is solely for idiomatic purposes.

class sure.core.DeepExplanation[source]

Add custom assertions, chains and chain properties

sure allows to add custom assertion methods, chain methods and chain properties.

Custom assertion methods

By default sure comes with a good amount of assertion methods. For example:

  • equals()

  • within()

  • contains()

And plenty more.

However, in some cases it makes sense to add custom assertion methods to improve the test experience.

Let’s assume you want to test your web application. Somewhere there is a Response class with a return_code property. We could do the following:

response = Response(...)

This is already quiet readable, but wouldn’t it be awesome do to something like this:

response = Response(...)

To achieve this the custom assertion methods come into play:

from sure import assertion

def return_code(self, expected_return_code):
    if self.negative:
        assert expected_return_code != self.obj.return_code, \
            'Expected return code matches'
        assert expected_return_code == self.obj.return_code, \
            'Expected return code does not match'

response = Response(...)

I’ll admit you have to write the assertion method yourself, but the result is a great experience you don’t want to miss.

Chain methods

chain methods are similar to assertion methods. The only difference is that the chain methods, as the name implies, can be chained with further chains or assertions:

from sure import chain

def header(self, header_name):
    # check if header name actually exists
    # return header value
    return self.obj.headers[header_name]

response = Response(200, headers={'Content-Type': 'text/python'})

Chain properties

chain properties are simple properties which are available to build an assertion. Some of the default chain properties are:

  • be

  • to

  • when

  • have

Use the chainproperty decorator like the following to build your own chain:

from sure import chainproperty, assertion

class Foo:
    magic = 42

def having(self):
    return self

def implement(self):
    return self

def attribute(self, name):
    has_it = hasattr(self.obj, name)
    if self.negative:
        assert not has_it, 'Expected was that object {0} does not have attr {1}'.format(
            self.obj, name)
        assert has_it, 'Expected was that object {0} has attr {1}'.format(
            self.obj, name)

# Build awesome assertion chains

Use custom assertion messages with ensure

With the ensure context manager sure provides an easy to use way to override the AssertionError message raised by sure’s assertion methods. See the following example:

import sure

name = myapi.do_something_that_returns_string()

with sure.ensure('the return value actually looks like: {0}', name):

In case name does not contain the string whatever it will raise an AssertionError exception with the message the return value actually looks like: <NAME> (where <NAME> would be the actual value of the variable name) instead of sure’s default error message in that particular case.

Only AssertionError exceptions are re-raised by sure.ensure() with the custom provided message. Every other exception will be ignored and handled as expected.