0

Let's say I have a function like that:

def foo():
  bar()

Is there any way I could test if bar is called, when I'm not passing bar as parameter? My team use Python 3.6 and 3.5

Ginko
  • 385
  • 1
  • 15
  • You could probably modify [these function call counting decorators](https://wiki.python.org/moin/PythonDecoratorLibrary#Counting_function_calls) to suit your purpose. – wwii Jun 04 '17 at 14:46
  • There are a number of alternatives, depending on your exact needs - search ```python decorator function logging```. Try one out and come back with questions about it if you get stuck. – wwii Jun 04 '17 at 14:49

1 Answers1

1

You should use patch for that:

@patch('path.to.bar')
def test_foo(self, mock_bar):
    foo() 
    self.assertTrue(mock_bar.called)

You can also test with which values the function was called like this:

mock_bar.assert_called_with('some_param')

Hope it helps.

Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91