1

I want to count the number of times that a function is called, python patch has been recommended allowing me to call call_count and things like assert_not_called to verify just that.

My problem is that I want the function to perform just as it did, as its function is required for the tests and it's on dynamic data so I cannot simply hardcode the result.

with patch.object(shutil, 'copy') as mm:

  do_some_things()

  mm.assert_not_called()

For do_some_things() to work correctly, shutil.copy still needs to perform its original role

hi im Bacon
  • 374
  • 1
  • 12
  • I don't think you are implementing the patch correctly. If implemented correctly, the shutil call is patched in which is is not actually called but instead mocked. You can mock the return of the function by using a side effect as well – l33tHax0r Nov 30 '19 at 04:10

1 Answers1

0

I would suggest just using patch not patch.object. You can still accomplish all you want with that. I don't having anything in ~/test

from unittest import TestCase
from unittest.mock import patch

from shutil import copy

def do_some_things(src, dst):
    copy(src, dst)


class TestDoSomething(TestCase):

    def test_do_somethings(self):
        with patch('test_do_some_things.copy') as mm:
            do_some_things('~/test.txt', '~/test/test.txt')
        mm.called_once()
        print(mm.call_count)
l33tHax0r
  • 1,384
  • 1
  • 15
  • 31