Fork me on GitHub

Deprecated Features

class Assert(*args)[source]

Wrap an object such that boolean operations on it fails with an AssertionError if the operation results in False, with more helpful error messages on failure than assert.

A test failure is simply an unhandled exception, so it is completely optional to use this class.

Examples:

Assert(1 + 1) == 2
2 in Assert([1, 2, 3])

Attributes are proxied to the wrapped object, returning the result wrapped as well:

hello = Assert('hello')
hello == 'hello'
hello.upper() == 'HELLO'
hello.capitalize() == 'Hello'

Used in boolean context, fails if non-true. These all fail:

bool(Assert(0))
if Assert(0): pass
assert Assert(0)

Identical to, except for the more helpful failure message:

Assert(bool(0)) == True

If you pass more than one argument, the first is assumed to be a predicate callable to which the rest of the arguments are passed. These are identical:

Assert.isinstance(0, int)
Assert(isinstance, 0, int)

Deprecated since version 0.5: assert_hook() is preferred.

obj

The wrapped object

Conditional tests

The normal conditional operators are supported:

  • Equality: == and !=
  • Comparison: <, <=, >, >=

Some keywords are also supported:

  • Containment: in, but beware that it is the container that should be wrapped and that the negated form, not in, will not work.

These operators and keywords are not natively supported:

  • Identity: is, is not
  • Negative containment: not in

They are instead supported via the following methods.

is_(obj)[source]

The is operator is not overridable, for good reasons (that would defeat its purpose), so you can use this method for asserting identity:

Assert(True).is_(True)

Changed in version 0.3: Checks the wrapped object for Assert instances.

is_not(obj)[source]

The negated form of is_(), corresponding to the is not operation:

Assert([]).is_not([])

Changed in version 0.3: Checks the wrapped object for Assert instances.

in_(obj)[source]

Assert membership. While you can use the in operator, its order is inconsistent with the rest of the operators and doesn’t work with the not in operation.

2 in Assert([1, 2, 3])
Assert(2).in_([1, 2, 3])
not_in(obj)[source]

The negated form of in_(), corresponding to the not in operation:

Assert(0).not_in([1, 2, 3])

Convinient helpers

json[source]

Parse the wrapped object as JSON. Requires Python 2.6 or the simplejson package.

New in version 0.4.

css(selector)[source]

Parse the wrapped object as HTML and return an assertive list of elements matching the CSS selector. Requires lxml 2.0 or newer.

Note

Not tested on Python 2.5 and PyPy due to difficulties installing lxml for these implementations.

New in version 0.4.

xpath(path)[source]

Parse the wrapped object as XML and return an assertive list of elements matching the XPath path. Requires lxml 2.0 or newer.

Note

Not tested on Python 2.5 and PyPy due to difficulties installing lxml for these implementations.

New in version 0.4.

Static methods

static raises(*exceptions)[source]

Context manager that fails if none of the exceptions are raised. Yields the captured exception as an assertive object.

with Assert.raises(IOError) as error:
    open('/etc/passwd', 'w')

error.errno == 13
Parameters:exceptions – Expected exception classes.
static not_raising(*exception)[source]

Context manager that fails if a particular exception is raised. A raised exception constitutes a failure anyway and this is mainly used for testing Attest itself.

with Assert.not_raising(IOError):
    open('/etc/passwd', 'r')
Parameters:exception – An exception class.
static isinstance(obj, classinfo)[source]

Test that an object is an instance of a class or a tuple() of classes. Corresponds to isinstance().

New in version 0.4.

static not_isinstance(obj, classinfo)[source]

Negated version of isinstance().

New in version 0.4.

static issubclass(obj, cls)[source]

Test that obj is a subclass of cls or a subclass of a class inside cls. Corresponds to issubclass().

New in version 0.4.

static not_issubclass(obj, cls)[source]

Negated version of issubclass().

New in version 0.4.

Proxying

Item and attribute access is proxied to the wrapped object, however in the latter case this can be unpredictable due to the wrapper class having its own attributes. Therefore there is a method for this, too.

attr(name)[source]

Safely get an attribute from the wrapped object.

New in version 0.4.

passed_to(func, *args, **kwargs)[source]

Pass the unwrapped object to a function and return its result as an assertive object.

These are identical:

Assert(len([1, 2, 3])) == 3
Assert([1, 2, 3]).passed_to(len) == 3

Mainly useful with Assert objects that comes from the outside, e.g. yielded from a context, from methods like css() etc.

New in version 0.4.

assert_(expr, msg=None)[source]

Like assert, but counts the assertion.

Deprecated since version 0.5: assert_hook() is preferred.

class Loader[source]

Run tests with Attest via distribute.

Deprecated since version 0.5: test_loader() is preferred.