0

Given the following unit test

@patch("requests.get")
def test_create_resource_success(self, request_get):
    request_get_mock.return_value = Mock(ok=True)
    request_get_mock.return_value.json.return_value = {'key': 'value'}
    success = create_resource(data)
    self.assertTrue(success)

How do I make sure that all other unit tests usages of requests.get are not patched? How do I reset the patching in the tearDown?

alegria
  • 1,290
  • 14
  • 23
  • 3
    you don't need to, that's literally what patch does – wim Sep 30 '20 at 21:26
  • @wim i noticed it's affecting my other unit tests, if i run all, some would fail because requests.get is still mocked. If I run the those affected failing tests individually they would succeed. – alegria Sep 30 '20 at 21:33
  • The problem is somewhere else in your test suite, not with this patch decorator. – wim Sep 30 '20 at 21:45
  • @alegria did you find a solution for your problem? – Leoncino Apr 12 '22 at 09:36
  • @Leoncino , i just remember wim is right the patch is doing it already, I believe I just fixed something else in my test. But I honestly can't remember now , apologies, i should have updated it here :( – alegria Apr 13 '22 at 09:33
  • 1
    No problem! I realized that my mock had been called within the same unittest, so I reset it by using ".reset_mock()" which can be applied to your patch :) Other than that it seems to be possible using patch.stop(), which stops the patch. – Leoncino Apr 14 '22 at 09:46

0 Answers0