Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.6KB

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