How to use _assert_raises_context_manager method in Testify

Best Python code snippet using Testify_python

assertions.py

Source:assertions.py Github

copy

Full Screen

...105 ... raise Exception106 >>> assert_raises(Exception, raise_exception, 1, kwarg=234)107 """108 if (len(args) == 1) and not kwargs:109 return _assert_raises_context_manager(args[0])110 else:111 return _assert_raises(*args, **kwargs)112def assert_raises_such_that(exception_class, exception_test=lambda e: e, callable_obj=None, *args, **kwargs):113 """114 Assert that an exception is raised such that expection_test(exception)115 passes, either in a with statement via a context manager or while calling116 a given callable on given arguments.117 Arguments:118 exception_class - class corresponding to the expected exception119 exception_test - a callable which takes an exception instance and120 asserts things about it121 callable_obj, *args, **kwargs - optional, a callable object and122 arguments to pass into it which when used are expected to raise the123 exception; if not provided, this function returns a context manager124 which will check that the assertion is raised within the context125 (the body of the with statement).126 As a context manager:127 >>> says_whatever = lambda e: assert_equal(str(e), "whatever")128 >>> with assert_raises_such_that(Exception, says_whatever):129 ... raise Exception("whatever")130 Pass in a callable:131 >>> says_whatever = lambda e: assert_equal(str(e), "whatever")132 >>> def raise_exception(arg, kwarg=None):133 ... raise Exception("whatever")134 >>> assert_raises_such_that(Exception, says_whatever, raise_exception, 1, kwarg=234)135 """136 if callable_obj is None:137 return _assert_raises_context_manager(exception_class, exception_test)138 else:139 with _assert_raises_context_manager(exception_class, exception_test):140 callable_obj(*args, **kwargs)141def assert_raises_exactly(exception_class, *args):142 """143 Assert that a particular exception_class is raised with particular arguments.144 Use this assertion when the exception message is important.145 """146 def test_exception(exception):147 # We want to know the exact exception type, not that it has some superclass.148 assert_is(type(exception), exception_class)149 assert_equal(exception.args, args)150 return assert_raises_such_that(exception_class, test_exception)151def assert_raises_and_contains(expected_exception_class, strings, callable_obj, *args, **kwargs):152 """Assert an exception is raised by passing in a callable and its153 arguments and that the string representation of the exception154 contains the case-insensitive list of passed in strings.155 Args156 strings -- can be a string or an iterable of strings157 """158 try:159 callable_obj(*args, **kwargs)160 except expected_exception_class as e:161 message = str(e).lower()162 try:163 is_string = isinstance(strings, basestring)164 except NameError:165 is_string = isinstance(strings, str)166 if is_string:167 strings = [strings]168 for string in strings:169 assert_in(string.lower(), message)170 else:171 assert_not_reached("No exception was raised (expected %s)" % expected_exception_class)172@contextlib.contextmanager173def _assert_raises_context_manager(exception_class, exception_test=lambda e: e):174 """Builds a context manager for testing that code raises an assertion.175 Args:176 exception_class - a subclass of Exception177 exception_test - optional, a function to apply to the exception (to178 test something about it)179 """180 try:181 yield182 except exception_class as e:183 exception_test(e)184 else:185 assert_not_reached("No exception was raised (expected %r)" %186 exception_class)187def _assert_raises(exception_class, callable, *args, **kwargs):188 with _assert_raises_context_manager(exception_class):189 callable(*args, **kwargs)190def _diff_message(lhs, rhs):191 """If `lhs` and `rhs` are strings, return the a formatted message192 describing their differences. If they're not strings, describe the193 differences in their `repr()`s.194 NOTE: Only works well for strings not containing newlines.195 """196 lhs = _to_characters(lhs)197 rhs = _to_characters(rhs)198 message = u'Diff:\nl: %s\nr: %s' % stringdiffer.highlight(lhs, rhs)199 # Python2 exceptions require bytes.200 if six.PY2:201 return message.encode('UTF-8')202 else:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testify automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful