How to use _assert_warns_context_manager method in Testify

Best Python code snippet using Testify_python

assertions.py

Source:assertions.py Github

copy

Full Screen

...557 if len(true_args) != 1:558 raise AssertionError("Expected exactly one True (got %d) args: %r" % (len(true_args), args))559 return true_args[0]560@contextlib.contextmanager561def _assert_warns_context_manager(warning_class=None, warnings_test=None):562 """563 Builds a context manager for testing code that should throw a warning.564 This will look for a given class, call a custom test, or both.565 Args:566 warning_class - a class or subclass of Warning. If not None, then567 the context manager will raise an AssertionError if the block568 does not throw at least one warning of that type.569 warnings_test - a function which takes a list of warnings caught,570 and makes a number of assertions about the result. If the function571 returns without an exception, the context manager will consider572 this a successful assertion.573 """574 with warnings.catch_warnings(record=True) as caught:575 # All warnings should be triggered.576 warnings.resetwarnings()577 if warning_class:578 warnings.simplefilter('ignore')579 warnings.simplefilter('always', category=warning_class)580 else:581 warnings.simplefilter('always')582 # Do something that ought to trigger a warning.583 yield584 # We should have received at least one warning.585 assert_gt(len(caught), 0, 'expected at least one warning to be thrown')586 # Run the custom test against the warnings we caught.587 if warnings_test:588 warnings_test(caught)589def assert_warns(warning_class=None, callable=None, *args, **kwargs):590 """Assert that the given warning class is thrown as a context manager591 or by passing in a callable and its arguments.592 As a context manager:593 >>> with assert_warns():594 ... warnings.warn('Hey!')595 Passing in a callable:596 >>> def throw_warning():597 ... warnings.warn('Hey!')598 >>> assert_warns(UserWarning, throw_warning)599 """600 if callable is None:601 return _assert_warns_context_manager(warning_class=warning_class)602 else:603 with _assert_warns_context_manager(warning_class=warning_class):604 callable(*args, **kwargs)605def assert_warns_such_that(warnings_test, callable=None, *args, **kwargs):606 """607 Assert that the given warnings_test function returns True when608 called with a full list of warnings that were generated by either609 a code block (when this is used as a context manager in a `with` statement)610 or the given callable (when called with the appropriate args and kwargs).611 As a context manager:612 >>> def two_warnings_thrown(warnings):613 ... assert len(warnings) == 2614 >>> with assert_warns_such_that(two_warnings_thrown):615 ... warnings.warn('Hey!')616 ... warnings.warn('Seriously!')617 Passing in a callable:618 >>> def throw_warnings(count):619 ... for n in range(count):620 ... warnings.warn('Warning #%i' % n)621 >>> assert_warns_such_that(two_warnings_thrown, throw_warnings, 2)622 """623 if callable is None:624 return _assert_warns_context_manager(warnings_test=warnings_test)625 else:626 with _assert_warns_context_manager(warnings_test=warnings_test):627 callable(*args, **kwargs)628def _to_characters(x):629 """Return characters that represent the object `x`, come hell or high water."""630 if isinstance(x, six.text_type):631 return x632 try:633 return six.text_type(x, 'UTF-8')634 except UnicodeDecodeError:635 return six.text_type(x, 'latin1')636 except TypeError:637 # We're only allowed to specify an encoding for str values, for whatever reason.638 try:639 return six.text_type(x)640 except UnicodeDecodeError:...

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