How to use assert_raises_such_that method in Testify

Best Python code snippet using Testify_python

assertions_test.py

Source:assertions_test.py Github

copy

Full Screen

...393 """Tests that the assertion fails when no exception is raised."""394 def exists(e):395 return True396 with assertions.assert_raises(AssertionError):397 with assertions.assert_raises_such_that(Exception, exists):398 pass399 def test_fails_when_wrong_exception_is_raised(self):400 """Tests that when an unexpected exception is raised, that it is401 passed through and the assertion fails."""402 def exists(e):403 return True404 # note: in assert_raises*, if the exception raised is not of the405 # expected type, that exception just falls through406 with assertions.assert_raises(Exception):407 with assertions.assert_raises_such_that(AssertionError, exists):408 raise Exception("the wrong exception")409 def test_fails_when_exception_test_fails(self):410 """Tests that when an exception of the right type that fails the411 passed in exception test is raised, the assertion fails."""412 def has_two_args(e):413 assertions.assert_length(e.args, 2)414 with assertions.assert_raises(AssertionError):415 with assertions.assert_raises_such_that(Exception, has_two_args):416 raise Exception("only one argument")417 def test_passes_when_correct_exception_is_raised(self):418 """Tests that when an exception of the right type that passes the419 exception test is raised, the assertion passes."""420 def has_two_args(e):421 assertions.assert_length(e.args, 2)422 with assertions.assert_raises_such_that(Exception, has_two_args):423 raise Exception("first", "second")424 def test_callable_is_called_with_all_arguments(self):425 """Tests that the callable form works properly, with all arguments426 passed through."""427 def message_is_foo(e):428 assert_equal(str(e), 'foo')429 class GoodArguments(Exception):430 pass431 arg1, arg2, kwarg = object(), object(), object()432 def check_arguments(*args, **kwargs):433 assert_equal((arg1, arg2), args)434 assert_equal({'kwarg': kwarg}, kwargs)435 raise GoodArguments('foo')436 assertions.assert_raises_such_that(GoodArguments, message_is_foo, check_arguments, arg1, arg2, kwarg=kwarg)437class AssertRaisesExactlyTestCase(TestCase):438 class MyException(ValueError):439 pass440 def test_passes_when_correct_exception_is_raised(self):441 with assertions.assert_raises_exactly(self.MyException, "first", "second"):442 raise self.MyException("first", "second")443 def test_fails_with_wrong_value(self):444 with assertions.assert_raises(AssertionError):445 with assertions.assert_raises_exactly(self.MyException, "first", "second"):446 raise self.MyException("red", "blue")447 def test_fails_with_different_class(self):448 class SpecialException(self.MyException):449 pass450 with assertions.assert_raises(AssertionError):451 with assertions.assert_raises_exactly(self.MyException, "first", "second"):452 raise SpecialException("first", "second")453 def test_fails_with_vague_class(self):454 with assertions.assert_raises(AssertionError):455 with assertions.assert_raises_exactly(Exception, "first", "second"):456 raise self.MyException("first", "second")457 def test_unexpected_exception_passes_through(self):458 class DifferentException(Exception):459 pass460 with assertions.assert_raises(DifferentException):461 with assertions.assert_raises_exactly(self.MyException, "first", "second"):462 raise DifferentException("first", "second")463class AssertRaisesAndContainsTestCase(TestCase):464 def test_fails_when_exception_is_not_raised(self):465 def raises_nothing():466 pass467 try:468 assertions.assert_raises_and_contains(ValueError, 'abc', raises_nothing)469 except AssertionError:470 pass471 else:472 assert_not_reached('AssertionError should have been raised')473 def test_fails_when_wrong_exception_is_raised(self):474 def raises_value_error():475 raise ValueError476 try:477 assertions.assert_raises_and_contains(MyException, 'abc', raises_value_error)478 except ValueError:479 pass480 else:481 assert_not_reached('ValueError should have been raised')482 def test_callable_is_called_with_all_arguments(self):483 class GoodArguments(Exception):484 pass485 arg1, arg2, kwarg = object(), object(), object()486 def check_arguments(*args, **kwargs):487 assert_equal((arg1, arg2), args)488 assert_equal({'kwarg': kwarg}, kwargs)489 raise GoodArguments('abc')490 assertions.assert_raises_and_contains(GoodArguments, 'abc', check_arguments, arg1, arg2, kwarg=kwarg)491 def test_fails_when_exception_does_not_contain_string(self):492 def raises_value_error():493 raise ValueError('abc')494 try:495 assertions.assert_raises_and_contains(ValueError, 'xyz', raises_value_error)496 except AssertionError:497 pass498 else:499 assert_not_reached('AssertionError should have been raised')500 def test_passes_when_exception_contains_string_with_matching_case(self):501 def raises_value_error():502 raise ValueError('abc')503 assertions.assert_raises_and_contains(ValueError, 'abc', raises_value_error)504 def test_passes_when_exception_contains_string_with_non_matching_case(self):505 def raises_value_error():506 raise ValueError('abc')507 assertions.assert_raises_and_contains(ValueError, 'ABC', raises_value_error)508 def test_passes_when_exception_contains_multiple_strings(self):509 def raises_value_error():510 raise ValueError('abc xyz')511 assertions.assert_raises_and_contains(ValueError, ['ABC', 'XYZ'], raises_value_error)512 def test_fails_when_exception_does_not_contains_all_strings(self):513 def raises_value_error():514 raise ValueError('abc xyz')515 try:516 assertions.assert_raises_and_contains(ValueError, ['ABC', '123'], raises_value_error)517 except AssertionError:518 pass519 else:520 assert_not_reached('AssertionError should have been raised')521class AssertDictSubsetTestCase(TestCase):522 def test_passes_with_subset(self):523 superset = {'one': 1, 'two': 2, 'three': 3}524 subset = {'one': 1}525 assert_dict_subset(subset, superset)526 def test_fails_with_wrong_key(self):527 superset = {'one': 1, 'two': 2, 'three': 3}528 subset = {'four': 4}529 assertions.assert_raises(AssertionError, assert_dict_subset, subset, superset)530 def test_fails_with_wrong_value(self):531 superset = {'one': 1, 'two': 2, 'three': 3}532 subset = {'one': 2}533 assertions.assert_raises(AssertionError, assert_dict_subset, superset, subset)534 def test_message_on_fail(self):535 superset = {'one': 1, 'two': 2, 'three': 3}536 subset = {'one': 2, 'two': 2}537 expected = "expected [subset has:{'one': 2}, superset has:{'one': 1}]"538 try:539 assert_dict_subset(subset, superset)540 except AssertionError as e:541 assert_equal(expected, e.args[0])542 else:543 assert_not_reached('AssertionError should have been raised')544class AssertEmptyTestCase(TestCase):545 def test_passes_on_empty_tuple(self):546 """Test that assert_empty passes on an empty tuple."""547 assertions.assert_empty(())548 def test_passes_on_empty_list(self):549 """Test that assert_empty passes on an empty list."""550 assertions.assert_empty([])551 def test_passes_on_unyielding_generator(self):552 """Test that assert_empty passes on an 'empty' generator."""553 def yield_nothing():554 if False:555 yield 0556 assertions.assert_empty(yield_nothing())557 def test_fails_on_nonempty_tuple(self):558 """Test that assert_empty fails on a nonempty tuple."""559 with assertions.assert_raises(AssertionError):560 assertions.assert_empty((0,))561 def test_fails_on_nonempty_list(self):562 """Test that assert_empty fails on a nonempty list."""563 with assertions.assert_raises(AssertionError):564 assertions.assert_empty([0])565 def test_fails_on_infinite_generator(self):566 """Tests that assert_empty fails on an infinite generator."""567 def yes():568 while True:569 yield 'y'570 with assertions.assert_raises(AssertionError):571 assertions.assert_empty(yes())572 def test_max_elements_to_print_eq_0_means_no_sample_message(self):573 """Tests that when max_elements_to_print is 0, there is no sample in the error message."""574 iterable = [1, 2, 3]575 expected_message = "iterable %s was unexpectedly non-empty." % iterable576 def message_has_no_sample(exception):577 assertions.assert_equal(str(exception), expected_message)578 with assertions.assert_raises_such_that(579 AssertionError, message_has_no_sample):580 assertions.assert_empty(iterable, max_elements_to_print=0)581 def test_max_elements_to_print_gt_len_means_whole_iterable_sample_message(self):582 """583 Tests that when max_elements_to_print is greater than the length of584 the whole iterable, the whole iterable is printed.585 """586 elements = [1, 2, 3, 4, 5]587 iterable = (i for i in elements)588 expected_message = "iterable %s was unexpectedly non-empty. elements: %s" \589 % (iterable, elements)590 def message_has_whole_iterable_sample(exception):591 assertions.assert_equal(str(exception), expected_message)592 with assertions.assert_raises_such_that(593 AssertionError, message_has_whole_iterable_sample):594 assertions.assert_empty(iterable, max_elements_to_print=len(elements) + 1)595 def test_max_elements_to_print_eq_len_means_whole_iterable_sample_message(self):596 """597 Tests that when max_elements_to_print is equal to the length of598 the whole iterable, the whole iterable is printed.599 """600 elements = [1, 2, 3, 4, 5]601 iterable = (i for i in elements)602 expected_message = "iterable %s was unexpectedly non-empty. elements: %s" \603 % (iterable, elements)604 def message_has_whole_iterable_sample(exception):605 assertions.assert_equal(str(exception), expected_message)606 with assertions.assert_raises_such_that(607 AssertionError, message_has_whole_iterable_sample):608 assertions.assert_empty(iterable, max_elements_to_print=len(elements))609 def test_max_elements_to_print_lt_len_means_partial_iterable_sample_message(self):610 """611 Tests that when max_elements_to_print is less than the length of the612 whole iterable, the first max_elements_to_print elements are printed.613 """614 elements = [1, 2, 3, 4, 5]615 iterable = (i for i in elements)616 max_elements_to_print = len(elements) - 1617 expected_message = "iterable %s was unexpectedly non-empty. first %i elements: %s" \618 % (iterable, max_elements_to_print, elements[:max_elements_to_print])619 def message_has_whole_iterable_sample(exception):620 assertions.assert_equal(str(exception), expected_message)621 with assertions.assert_raises_such_that(622 AssertionError, message_has_whole_iterable_sample):623 assertions.assert_empty(iterable, max_elements_to_print=max_elements_to_print)624class AssertNotEmptyTestCase(TestCase):625 def test_fails_on_empty_tuple(self):626 with assertions.assert_raises(AssertionError):627 assertions.assert_not_empty(())628 def test_fails_on_empty_list(self):629 """Test that assert_not_empty fails on an empty list."""630 with assertions.assert_raises(AssertionError):631 assertions.assert_not_empty([])632 def test_fails_on_unyielding_generator(self):633 """Test that assert_not_empty fails on an 'empty' generator."""634 def yield_nothing():635 if False:...

Full Screen

Full Screen

test_stream.py

Source:test_stream.py Github

copy

Full Screen

...210 assert_equal(s._partition_key({'ünîcødé_πå®tîtîøñ_ke¥_宇宙': 'ünîcødé_πå®tîtîøñ_√al_宇宙'}), u'ünîcødé_πå®tîtîøñ_√al_宇宙')211 def test_missing_ascii_partition_key(self):212 c = turtle.Turtle()213 s = stream.Stream(c, 'test stream', 'value')214 with assert_raises_such_that(KeyError, lambda ke: assert_equal(ke.args[0], 'value')):215 s._partition_key({'wrong_value': 'ascii_string'})216 def test_missing_unicode_partition_key(self):217 c = turtle.Turtle()218 s = stream.Stream(c, u'test_üñîçø∂é_stream', u'ünîcødé_πå®tîtîøñ_ke¥_宇宙')219 #NOTE: We should test that, when partition key is missing from data, we return220 #a KeyError with the same unicode parition_key object that is a member of the Stream object221 with assert_raises_such_that(KeyError, lambda ke: assert_equal(ke.args[0], u'ünîcødé_πå®tîtîøñ_ke¥_宇宙')):222 s._partition_key({u'wrong_ünîcødé_πå®tîtîøñ_ke¥_宇宙': u'ünîcødé_πå®tîtîøñ_√al_宇宙'})223 with assert_raises_such_that(KeyError, lambda ke: assert_equal(ke.args[0], u'ünîcødé_πå®tîtîøñ_ke¥_宇宙')):224 s._partition_key({'wrong_ünîcødé_πå®tîtîøñ_ke¥_宇宙': 'ünîcødé_πå®tîtîøñ_√al_宇宙'})225 def test_missing_escaped_unicode_partition_key(self):226 c = turtle.Turtle()227 s = stream.Stream(c, 'test_üñîçø∂é_stream', 'ünîcødé_πå®tîtîøñ_ke¥_宇宙')228 #NOTE: We should test that, when partition key is missing from data, we return229 #a KeyError with the same unicode parition_key object that is a member of the Stream object230 with assert_raises_such_that(KeyError, lambda ke: assert_equal(ke.args[0], u'ünîcødé_πå®tîtîøñ_ke¥_宇宙')):231 s._partition_key({u'wrong_ünîcødé_πå®tîtîøñ_ke¥_宇宙': u'ünîcødé_πå®tîtîøñ_√al_宇宙'})232 with assert_raises_such_that(KeyError, lambda ke: assert_equal(ke.args[0], u'ünîcødé_πå®tîtîøñ_ke¥_宇宙')):233 s._partition_key({'wrong_ünîcødé_πå®tîtîøñ_ke¥_宇宙': 'ünîcødé_πå®tîtîøñ_√al_宇宙'})234 def test_shards_ids(self):235 c = turtle.Turtle()236 def describe_stream(_):237 return {238 'StreamDescription': {239 'HasMoreShards': False,240 'Shards': [241 {'ShardId': '0001'}, {'ShardId': '0002'}242 ]243 }244 }245 c.describe_stream = describe_stream246 s = stream.Stream(c, 'test stream', 'value')...

Full Screen

Full Screen

common_test.py

Source:common_test.py Github

copy

Full Screen

...4 class A(object): pass5 class B(A): pass6 class C(object): pass7 def test_issubclass_not_a_subclass(self):8 with T.assert_raises_such_that(9 AssertionError,10 lambda e:11 e.args == (12 'Expected %s to be a subclass of %s' % (13 self.C.__name__, self.A.__name__14 ),15 )16 ):17 assert_issubclass(self.C, self.A)18 def test_issubclass_actually_a_subclass(self):...

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