Skip to main content

An easy-to-use assertion library for Python

Project description


An easy-to-use assertion library for python.

Quick Start

Install insist

$ pip install insist


$ sudo pip install insist

Create Insist object

from insist import Insist

insist = Insist()

# Raise RuntimeError instead of AssertionError
insist_rt = Insist(RuntimeError)

# Prefix all messages with "OOPS : "
insist_prefix = Insist(message_prefix="OOPS")

# Show both custom and standard error messages
# Can be combined with message_prefix
insist_both = Insist(always_show_standard=True)

Make assertions

insist.true(1 == 1) # Valid assertion

insist(1 == 1) # Same as insist.true(expr)

insist(1 == 2) # Raises AssertionError

# Raises AssertionError with custom error message
insist(1 == 2, "Danger, Will Robinson")

# Raises AssertionError with custom and standard error message joined with " : "
insist_both(1 == 2, "Danger, Will Robinson")


All tests

from insist import Insist

insist = Insist()

insist(x) # x == True

insist.true(x) # x == True

insist.false(x) # x == False

insist.always(custom) # Always raise an error

insist.equal(x, y) # x == y

insist.not_equal(x, y) # x != y

insist.less(x, y) # x < y

insist.less_equal(x, y) # x <= y

insist.greater(x, y) # x > y

insist.greater_equal(x, y) # x >= y

insist.is_same(x, y) # x is y

insist.is_not(x, y) # x is not y

insist.is_in(x, y) # x in y

insist.not_in(x, y) # x not in y

insist.is_subclass(x, y) # issubclass(x, y)

insist.is_not_subclass(x, y) # not issubclass(x, y)

insist.isa(x, y) # isinstance(x, y)

insist.is_not_a(x, y) # not isinstance(x, y)

insist.is_string(x, y) # isinstance(x, str) for Py3
# isinstance(x, (str, unicode)) for Py2

insist.is_not_string(x, y) # not isinstance(x, str) for Py3
# not isinstance(x, (str, unicode)) for Py2

insist.keys(x, required, optional, extra, custom) # See examples below

# Dict used for remaining examples
x = { 'a' : 1, 'b' : 2, 'c' : 3 }

# Require that x has listed keys with additional keys allowed.
insist.keys(x, required=['a', 'b']) # OK
insist.keys(x, required=['a', 'd']) # Raises error

# Require that x has listed keys with additional keys not allowed.
insist.keys(x, required=['a', 'b'], extra=False) # Raises error

# Require that x has listed keys but no keys other than optional keys allowed.
insist.keys(x, required=['a', 'b'], optional=['c']) # OK
insist.keys(x, required=['a', 'b'], optional=['d']) # Raises error

Chainable Tests

The "that" method retains its value and uses it as the first value for the remaining methods in chain.
This simplifies the testing of a single value against multiple criteria.

from insist import Insist

insist = Insist()

x = 3

insist.that(x).isa(int).greater_equal(0).less(10) # OK if x is integer from 0 to 9

# The result of the "that" method may be stored in a variable as well.

say_x = insist.that(x)



# In addition to the methods listed above, a "that chain" has two additional methods.
# The has and not_has methods are similar to is_in and not_in except that the
# arguments are reversed.

x = [ 1, 2, 3 ]

say_x = insist.that(x)

say_x.has(1).has(2) # OK
say_x.has(1).not_has(2) # Raises error
say_x.has(1).has(5) # Raises error

x = { 'a' : 1, 'b' : 2, 'c' : 3 }

say_x = insist.that(x)

say_x.has('a').has('b') # OK
say_x.has('a').has('d') # Raises error

Copyright and License

Copyright 2014 Ray Harris

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this module except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

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

insist-0.2.tar.gz (5.1 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