Python: class Assertions

assertions = class Assertions(__builtin__.object)
`` |  | Methods defined here: assert_call_nothing_raised(self, *args, **kw_args) `Deprecated. Use assert_nothing_raised_call().` assert_call_raise(self, *args, **kw_args) `Deprecated. Use assert_raise_call().` assert_callable(self, object, message=None) `Passes if callable(object) returns True. assert_callable(lambda: 1) # => pass assert_callable("string") # => fail` assert_equal(self, expected, actual, message=None) `Passes if expected == actual. assert_equal(5, 2 + 3) # => pass` assert_exists(self, path) `Passes if path exists. assert_exists("/tmp/exist") # => pass assert_exists("/tmp/nonexistence") # => fail` assert_false(self, expression, message=None) `Passes if expression is false value. assert_false(False) # => pass assert_false("") # => pass` assert_hasattr(self, object, name, message=None) `Passes if hasattr(object, name) returns True. assert_hasattr("string", "strip") # => pass assert_hasattr("string", "unknown") # => fail` assert_in_delta(self, expected, actual, delta, message=None) `Passes if (expected - delta) <= actual <= (expected + delta). assert_in_delta(3, 3.01, 0.001) # => pass` assert_kernel_symbol(self, name) `Passes if /proc/kallsyms can be opened and name is in the list. assert_kernel_symbol("printk") # => pass # returns an address of printk assert_kernel_symbol("non_existent") # => fail` assert_match(self, pattern, target, message=None) `Passes if re.match(pattern, target) doesn't return None. assert_match("abc", "abcde") # => pass assert_match("abc", "deabc") # => fail` assert_none(self, expression, message=None) `Passes if expression is None. assert_none(None) # => pass` assert_not_equal(self, not_expected, actual, message=None) `Passes if not_expected != actual. assert_not_equal(-5, 2 + 3) # => pass` assert_not_exists(self, path) `Passes if path doesn't exists. assert_not_exists("/tmp/nonexistence") # => pass assert_not_exists("/tmp/exist") # => fail` assert_not_found(self, pattern, target, message=None) `Passes if re.search(pattern, target) returns None. assert_search("abc", "deABC") # => pass assert_search("abc", "deabc") # => fail` assert_not_match(self, pattern, target, message=None) `Passes if re.match(pattern, target) returns None. assert_not_match("abc", "deabc") # => pass assert_not_match("abc", "abcde") # => fail` assert_not_none(self, expression, message=None) `Passes if expression is not None. assert_not_none("not none") # => pass` assert_nothing_raised_call(self, callable_object, *args, **kw_args) `Passes if callable_object(*args, **kw_args) raises nothing exception and returns called result. assert_nothing_raised_call(lambda: 1) # => pass # => returns 1 assert_nothing_raised_call(lambda: unknown_variable) # => fail` assert_open_file(self, name, *args) `Passes if open(name, *args) succeeds. file = assert_open_file("/tmp/exist", "w") # => pass assert_open_file("/tmp/nonexistence") # => fail` assert_raise_call(self, exception, callable_object, *args, **kw_args) `Passes if callable_object(*args, **kw_args) raises exception and returns raised exception value. assert_raise_call(NameError, lambda: unknown_variable) # => pass # => returns NameError # value assert_raise_call(NameError, lambda: 1) # => fail Exception instance can be also passed if it's comparable. class ComparableError(Exception): def __init__(self, message): self.message = message def __repr__(self): return "%s(%r,)" % (type(self).__name__, self.message) def __eq__(self, other): return isinstance(other, self.__class__) and self.message == other.message def raise_error(): raise ComparableError("value") assert_raise_call(ComparableError("value"), raise_error) # => pass # => returns # ComparableError # value assert_raise_call(ComparableError("key"), raise_error) # => fail` assert_run_command(self, command, **kw_args) `Passes if command is successfully ran and returns subprocess.Popen. process = assert_run_command(["echo", "123"]) # => pass assert_equal("123\n", process.stdout.read()) # => pass assert_run_command("false") # => fail assert_run_command("unknown-command") # => fail` assert_search(self, pattern, target, message=None) `Passes if re.search(pattern, target) doesn't return None. assert_search("abc", "deabc") # => pass assert_search("abc", "deABC") # => fail` assert_search_syslog_call(self, pattern, callable_object, *args, **kw_args) `Passes if re.search(pattern, SYSLOG_CONTENT) doesn't return None after callable_object(*args, **kw_args). assert_search_syslog_call("X", syslog.syslog, "XYZ") # => pass assert_search_syslog_call("X", syslog.syslog, "ABC") # => fail` assert_true(self, expression, message=None) `Passes if expression is true value. assert_true(True) # => pass assert_true("string") # => pass` assert_try_call(self, timeout, interval, callable_object, *args, **kw_args) `Passes if callable_object(*args, **kw_args) doesn't fail any assertions in <timeout> seconds. (It will tried <timeout / interval> times.) def random_number(): number = random.randint(0, 9) assert_in_delta(5, number, 1) return number assert_try_call(1, 0.1, random_number) # => will pass # returns 4, 5 or 6 assert_try_call(1, 0.1, self.fail, "Never succeed") # => fail` fail(self, message) `Always fails with message. fail("must not happen!") # => fail` notify(self, message) `Notify a message for the current running test. if command_not_found: notify("skip due to command not found") # => notify a message return` omit(self, message) `Omit the current running test. if module_not_found: omit("omit due to a module isn't found") # => omit test` pend(self, message) `Pend the current running test. pend("module XXX isn't found") # => pend test` Data descriptors defined here: __dict__ `dictionary for instance variables (if defined)` __weakref__ `list of weak references to the object (if defined)`