From 4f87776d4f7b49ef4043de1fd3e92808e07ada70 Mon Sep 17 00:00:00 2001 From: Francesco Faraone Date: Mon, 19 Jun 2023 17:58:55 +0200 Subject: [PATCH] Wrap mocker stop within try/finally --- connect/client/testing/fluent.py | 6 ++++-- tests/client/test_testing.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/connect/client/testing/fluent.py b/connect/client/testing/fluent.py index 37c5168..1e11f8b 100644 --- a/connect/client/testing/fluent.py +++ b/connect/client/testing/fluent.py @@ -153,8 +153,10 @@ def start(self): _mocker.start() def reset(self, success=True): - _mocker.stop(allow_assert=success) - _mocker.reset() + try: + _mocker.stop(allow_assert=success) + finally: + _mocker.reset() def __enter__(self): self.start() diff --git a/tests/client/test_testing.py b/tests/client/test_testing.py index d24fc3f..fe700df 100644 --- a/tests/client/test_testing.py +++ b/tests/client/test_testing.py @@ -395,3 +395,15 @@ def test_exclude(mocker, exclude): def test_get_requests_mocker(): assert get_requests_mocker() == _mocker + + +def test_mocker_reset(mocker): + mocker.patch( + 'connect.client.testing.fluent._mocker.stop', + side_effect=Exception('error'), + ) + mocked_reset = mocker.patch('connect.client.testing.fluent._mocker.reset') + mocker = ConnectClientMocker('http://localhost') + with pytest.raises(Exception): + mocker.reset() + mocked_reset.assert_called_once()