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.
The wrapped object
Conditional tests
The normal conditional operators are supported:
Some keywords are also supported:
These operators and keywords are not natively supported:
They are instead supported via the following methods.
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.
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.
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])
The negated form of in_(), corresponding to the not in operation:
Assert(0).not_in([1, 2, 3])
Convinient helpers
Parse the wrapped object as JSON. Requires Python 2.6 or the simplejson package.
New in version 0.4.
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.
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
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. |
---|
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. |
---|
Test that an object is an instance of a class or a tuple() of classes. Corresponds to isinstance().
New in version 0.4.
Negated version of isinstance().
New in version 0.4.
Test that obj is a subclass of cls or a subclass of a class inside cls. Corresponds to issubclass().
New in version 0.4.
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.
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.
Like assert, but counts the assertion.
Deprecated since version 0.5: assert_hook() is preferred.
Run tests with Attest via distribute.
Deprecated since version 0.5: test_loader() is preferred.