You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_common.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Tests for hyperlink.test.common
  3. """
  4. from unittest import TestCase
  5. from .common import HyperlinkTestCase
  6. class _ExpectedException(Exception):
  7. """An exception used to test HyperlinkTestCase.assertRaises.
  8. """
  9. class _UnexpectedException(Exception):
  10. """An exception used to test HyperlinkTestCase.assertRaises.
  11. """
  12. class TestHyperlink(TestCase):
  13. """Tests for HyperlinkTestCase"""
  14. def setUp(self):
  15. self.hyperlink_test = HyperlinkTestCase("run")
  16. def test_assertRaisesWithCallable(self):
  17. """HyperlinkTestCase.assertRaises does not raise an AssertionError
  18. when given a callable that, when called with the provided
  19. arguments, raises the expected exception.
  20. """
  21. called_with = []
  22. def raisesExpected(*args, **kwargs):
  23. called_with.append((args, kwargs))
  24. raise _ExpectedException
  25. self.hyperlink_test.assertRaises(_ExpectedException,
  26. raisesExpected, 1, keyword=True)
  27. self.assertEqual(called_with, [((1,), {"keyword": True})])
  28. def test_assertRaisesWithCallableUnexpectedException(self):
  29. """When given a callable that raises an unexpected exception,
  30. HyperlinkTestCase.assertRaises raises that exception.
  31. """
  32. def doesNotRaiseExpected(*args, **kwargs):
  33. raise _UnexpectedException
  34. try:
  35. self.hyperlink_test.assertRaises(_ExpectedException,
  36. doesNotRaiseExpected)
  37. except _UnexpectedException:
  38. pass
  39. def test_assertRaisesWithCallableDoesNotRaise(self):
  40. """HyperlinkTestCase.assertRaises raises an AssertionError when given
  41. a callable that, when called, does not raise any exception.
  42. """
  43. def doesNotRaise(*args, **kwargs):
  44. return True
  45. try:
  46. self.hyperlink_test.assertRaises(_ExpectedException,
  47. doesNotRaise)
  48. except AssertionError:
  49. pass
  50. def test_assertRaisesContextManager(self):
  51. """HyperlinkTestCase.assertRaises does not raise an AssertionError
  52. when used as a context manager with a suite that raises the
  53. expected exception. The context manager stores the exception
  54. instance under its `exception` instance variable.
  55. """
  56. with self.hyperlink_test.assertRaises(_ExpectedException) as cm:
  57. raise _ExpectedException
  58. self.assertTrue(isinstance(cm.exception, _ExpectedException))
  59. def test_assertRaisesContextManagerUnexpectedException(self):
  60. """When used as a context manager with a block that raises an
  61. unexpected exception, HyperlinkTestCase.assertRaises raises
  62. that unexpected exception.
  63. """
  64. try:
  65. with self.hyperlink_test.assertRaises(_ExpectedException):
  66. raise _UnexpectedException
  67. except _UnexpectedException:
  68. pass
  69. def test_assertRaisesContextManagerDoesNotRaise(self):
  70. """HyperlinkTestcase.assertRaises raises an AssertionError when used
  71. as a context manager with a block that does not raise any
  72. exception.
  73. """
  74. try:
  75. with self.hyperlink_test.assertRaises(_ExpectedException):
  76. pass
  77. except AssertionError:
  78. pass