FAILURE_EXCEPTION = AssertionError def fail(msg=None): """Fail immediately, with the given message.""" raise FAILURE_EXCEPTION, msg def assert_false(expr, msg=None): "Fail the test if the expression is true." if expr: raise FAILURE_EXCEPTION, msg def assert_true(expr, msg=None): """Fail the test unless the expression is true.""" if not expr: raise FAILURE_EXCEPTION, msg def 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. """ try: callableObj(*args, **kwargs) except excClass: return else: if hasattr(excClass,'__name__'): excName = excClass.__name__ else: excName = str(excClass) raise FAILURE_EXCEPTION, "%s not raised" % excName def assert_equal(first, second, msg=None): """Fail if the two objects are unequal as determined by the '==' operator. """ if not first == second: raise FAILURE_EXCEPTION, (msg or '%r != %r' % (first, second)) def asssert_not_equal(first, second, msg=None): """Fail if the two objects are equal as determined by the '==' operator. """ if first == second: raise FAILURE_EXCEPTION, (msg or '%r == %r' % (first, second)) def assert_almost_equal(first, second, places=7, 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). """ if round(second-first, places) != 0: raise FAILURE_EXCEPTION, \ (msg or '%r != %r within %r places' % (first, second, places)) def assert_not_almost_equal(first, second, places=7, 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). """ if round(second-first, places) == 0: raise FAILURE_EXCEPTION, \ (msg or '%r == %r within %r places' % (first, second, places))