def test_do_notify_exception(monkeypatch): notif_mock = MagicMock() monkeypatch.setattr(Action, "notify", notif_mock) action = Action(MagicMock(side_effect=Exception)) try: action.do() except Exception: pass assert notif_mock.call_count == 2
def test_context_manager_retry(): mock_action = MagicMock(side_effect=Exception) action = Action(mock_action) @action.action_context_manager @contextmanager def retry_do_3_times_if_it_fails(action): try: yield except Exception: action.retries = getattr(action, "retries", 0) + 1 if action.retries < 3: action.do() else: raise with pytest.raises(Exception): action.do() assert mock_action.call_count == 3
def test_classic_context_manager(): # Purpose of this test is to check that classic context manager works too action = Action(action_fct=lambda: 10) @action.action_context_manager class ContextManager(object): def __init__(self, action): self.action = action def __enter__(self): # May use self.action pass def __exit__(self, *args, **kwargs): # May use self.action pass ContextManager.__enter__ = MagicMock() ContextManager.__exit__ = MagicMock() action.do() assert ContextManager.__enter__.called assert ContextManager.__exit__.called
def test_do_notify(monkeypatch): notif_mock = MagicMock() monkeypatch.setattr(Action, "notify", notif_mock) action = Action(MagicMock()) action.do() assert notif_mock.call_count == 2