def _( func=each( assert_equal, assert_not_equal, assert_in, assert_not_in, assert_less_than, assert_less_than_equal_to, assert_greater_than, assert_greater_than_equal_to, ), lhs=each(1, 1, "a", "a", 2, 2, 1, 1), rhs=each(2, 1, "b", "a", 1, 1, 2, 2), ): with raises(TestFailure): func(lhs, rhs, "")
def _(): import traceback def raising(): raise ValueError def dangerous(): raising() try: dangerous() except ValueError as err: try_tb = traceback.extract_tb(err.__traceback__) with raises(ValueError) as ctx: dangerous() raises_tb = traceback.extract_tb(ctx.raised.__traceback__) assert try_tb[1:] == raises_tb[1:]
def _(): with raises(ValueError) as ctx: raise ValueError("xyz") assert "y" in str(ctx.raised)
def _(): err = ValueError("x") with raises(ValueError) as ctx: raise err assert ctx.raised is err
def _(): with raises(ValueError): raise ValueError
def _(): with raises(AssertionError): with raises(ValueError): raise RuntimeError
def _(): class SubClass(ValueError): pass with raises(ValueError): raise SubClass()
def _(): assert_lineno = inspect.currentframe().f_lineno + 2 with raises(TestFailure) as ctx: assert_equal(0, 1, "msg") assert ctx.raised.error_line == assert_lineno
def _(): with raises(ValueError): pass