nose: nose.tools

nose.tools provides a few convenience functions to make writing tests easier. You don't have to use them; nothing in the rest of nose depends on any of these methods.

Classes

Highlighted methods are defined in this class.

TimeExpired (exceptions.AssertionError)

Methods

__getitem__(...)(inherited from BaseException)

x.__getitem__(y) <==> x[y]

__getslice__(...)(inherited from BaseException)

x.__getslice__(i, j) <==> x[i:j]

Use of negative indices is not supported.

__init__(...)(inherited from AssertionError)

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

__setstate__(...)(inherited from BaseException)

Attributes

args
Default value: <attribute 'args' of 'exceptions.BaseException' objects>
message
Default value: <member 'message' of 'exceptions.BaseException' objects>

Functions

assert_true(expr, msg)

Fail the test unless the expression is true.

assert_equal(first, second, msg)

Fail if the two objects are unequal as determined by the '==' operator.

eq_(a, b, msg=None)

Shorthand for 'assert a == b, "%r != %r" % (a, b)

set_trace()

Call pdb.set_trace in the calling frame, first restoring sys.stdout to the real output stream. Note that sys.stdout is NOT reset to whatever it was before the call once pdb is done!

assert_almost_equals(first, second, places, msg=None)

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

assert_equals(first, second, msg)

Fail if the two objects are unequal as determined by the '==' operator.

raises(*exceptions)

Test must raise one of expected exceptions to pass.

Example use:

@raises(TypeError, ValueError)
def test_raises_type_error():
    raise TypeError("This test passes")

@raises(Exception):
def test_that_fails_by_passing():
    pass

If you want to test many assertions about exceptions in a single test, you may want to use assert_raises instead.

make_decorator(func)

Wraps a test decorator so as to properly replicate metadata of the decorated function, including nose's additional stuff (namely, setup and teardown).

with_setup(setup=None, teardown=None)

Decorator to add setup and/or teardown methods to a test function:

@with_setup(setup, teardown)
def test_something():
    # ...

Note that with_setup is useful only for test functions, not for test methods or inside of TestCase subclasses.

assert_almost_equal(first, second, places, msg=None)

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

assert_not_equal(first, second, msg)

Fail if the two objects are equal as determined by the '==' operator.

assert_not_almost_equals(first, second, places, msg=None)

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

assert_not_equals(first, second, msg)

Fail if the two objects are equal as determined by the '==' operator.

assert_raises(excClass, callableObj, *args, **kwargs)

Fail unless an exception of class excClass is thrown by callableObj when invoked with arguments args and keyword arguments kwargs. If a different type of exception is thrown, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

ok_(expr, msg=None)

Shorthand for assert. Saves 3 whole characters!

timed(limit)

Test must finish within specified time limit to pass.

Example use:

@timed(.1)
def test_that_fails():
    time.sleep(.2)
assert_not_almost_equal(first, second, places, msg=None)

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

assert_false(expr, msg)

Fail the test if the expression is true.