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.

common.py 2.4KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from typing import Any, Callable, Optional, Type
  2. from unittest import TestCase
  3. class HyperlinkTestCase(TestCase):
  4. """This type mostly exists to provide a backwards-compatible
  5. assertRaises method for Python 2.6 testing.
  6. """
  7. def assertRaises( # type: ignore[override]
  8. self,
  9. expected_exception, # type: Type[BaseException]
  10. callableObj=None, # type: Optional[Callable[..., Any]]
  11. *args, # type: Any
  12. **kwargs # type: Any
  13. ):
  14. # type: (...) -> Any
  15. """Fail unless an exception of class expected_exception is raised
  16. by callableObj when invoked with arguments args and keyword
  17. arguments kwargs. If a different type of exception is
  18. raised, it will not be caught, and the test case will be
  19. deemed to have suffered an error, exactly as for an
  20. unexpected exception.
  21. If called with callableObj omitted or None, will return a
  22. context object used like this::
  23. with self.assertRaises(SomeException):
  24. do_something()
  25. The context manager keeps a reference to the exception as
  26. the 'exception' attribute. This allows you to inspect the
  27. exception after the assertion::
  28. with self.assertRaises(SomeException) as cm:
  29. do_something()
  30. the_exception = cm.exception
  31. self.assertEqual(the_exception.error_code, 3)
  32. """
  33. context = _AssertRaisesContext(expected_exception, self)
  34. if callableObj is None:
  35. return context
  36. with context:
  37. callableObj(*args, **kwargs)
  38. class _AssertRaisesContext(object):
  39. "A context manager used to implement HyperlinkTestCase.assertRaises."
  40. def __init__(self, expected, test_case):
  41. # type: (Type[BaseException], TestCase) -> None
  42. self.expected = expected
  43. self.failureException = test_case.failureException
  44. def __enter__(self):
  45. # type: () -> "_AssertRaisesContext"
  46. return self
  47. def __exit__(self, exc_type, exc_value, tb):
  48. # type: (Optional[Type[BaseException]], Any, Any) -> bool
  49. if exc_type is None:
  50. exc_name = self.expected.__name__
  51. raise self.failureException("%s not raised" % (exc_name,))
  52. if not issubclass(exc_type, self.expected):
  53. # let unexpected exceptions pass through
  54. return False
  55. self.exception = exc_value # store for later retrieval
  56. return True