frostfs-testlib/tests/test_reporter.py
Vladimir Domnich 834ddede36 [#5] Remove testlib config file support
In order to make library as flexible as possible we will try to use
configuration methods similar to function `logging.dictConfig` from the
standard library. So, we won't support configuration file
`.neofs-testlib.yaml`, but will allow users to call `configure` method
that will load plugins and initialize library components.

Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
2022-10-07 15:05:48 +04:00

73 lines
2.3 KiB
Python

from contextlib import AbstractContextManager
from types import TracebackType
from typing import Optional
from unittest import TestCase
from unittest.mock import MagicMock
from neofs_testlib.reporter import Reporter
class TestLocalShellInteractive(TestCase):
def setUp(self):
self.reporter = Reporter()
def test_handler_step_is_invoked(self):
handler = MagicMock()
self.reporter.register_handler(handler)
with self.reporter.step("test_step"):
pass
handler.step.assert_called_once_with("test_step")
def test_two_handler_steps_are_invoked(self):
handler1 = MagicMock()
handler2 = MagicMock()
self.reporter.register_handler(handler1)
self.reporter.register_handler(handler2)
with self.reporter.step("test_step"):
pass
handler1.step.assert_called_once_with("test_step")
handler2.step.assert_called_once_with("test_step")
def test_handlers_can_suppress_exception(self):
handler1 = MagicMock()
handler1.step = MagicMock(return_value=StubContext(suppress_exception=True))
handler2 = MagicMock()
handler2.step = MagicMock(return_value=StubContext(suppress_exception=True))
self.reporter.register_handler(handler1)
self.reporter.register_handler(handler2)
with self.reporter.step("test_step"):
raise ValueError("Test exception")
def test_handler_can_override_exception_suppression(self):
handler1 = MagicMock()
handler1.step = MagicMock(return_value=StubContext(suppress_exception=True))
handler2 = MagicMock()
handler2.step = MagicMock(return_value=StubContext(suppress_exception=False))
self.reporter.register_handler(handler1)
self.reporter.register_handler(handler2)
with self.assertRaises(ValueError):
with self.reporter.step("test_step"):
raise ValueError("Test exception")
class StubContext(AbstractContextManager):
def __init__(self, suppress_exception: bool) -> None:
super().__init__()
self.suppress_exception = suppress_exception
def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
return self.suppress_exception