Best Python code snippet using Testify_python
assertions_test.py
Source:assertions_test.py
...369 def test_passes_when_exception_is_raised(self):370 def raises_value_error():371 raise ValueError372 assertions.assert_raises(ValueError, raises_value_error)373 def test_fails_when_wrong_exception_is_raised(self):374 def raises_value_error():375 raise ValueError376 try:377 assertions.assert_raises(MyException, raises_value_error)378 except ValueError:379 pass380 else:381 assert_not_reached('ValueError should have been raised')382 def test_callable_is_called_with_all_arguments(self):383 class GoodArguments(Exception):384 pass385 arg1, arg2, kwarg = object(), object(), object()386 def check_arguments(*args, **kwargs):387 assert_equal((arg1, arg2), args)388 assert_equal({'kwarg': kwarg}, kwargs)389 raise GoodArguments390 assertions.assert_raises(GoodArguments, check_arguments, arg1, arg2, kwarg=kwarg)391class AssertRaisesSuchThatTestCase(TestCase):392 def test_fails_when_no_exception_is_raised(self):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)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!