title: How to check if a function was called in a unit test using pytest-mock?
body: <p>I have a unit test where I want to check if a function was called. How do I do this with<code>pytest</code> and <code>pytest-mock</code> libraries?</p>
  
  <p>For example, here is a unit test <code>test_hello.py</code>. In this test I call the function <code>my_function</code> and want to verify that it called <code>hello</code> with a given argument.</p>
  
  <pre><code>def hello(name):
      return f'Hello {name}'
  
  def my_function():
      hello('Sam')
  
  def test_hello(mocker):
      mocker.patch('hello')
      my_function()
      hello.assert_called_once_with('Sam')
  </code></pre>
  
  <p>The code above returns the following error:</p>
  
  <pre><code>target = 'hello'
  
      def _get_target(target):
          try:
  &gt;           target, attribute = target.rsplit('.', 1)
  E           ValueError: not enough values to unpack (expected 2, got 1)
  
  /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py:1393: ValueError
  
  During handling of the above exception, another exception occurred:
  
  mocker = &lt;pytest_mock.MockFixture object at 0x109c5e978&gt;
  
      def test_hello(mocker):
  &gt;       mocker.patch('hello')
  
  test_hello.py:8: 
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pytest_mock.py:156: in __call__
      return self._start_patch(self.mock_module.patch, *args, **kwargs)
  /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pytest_mock.py:134: in _start_patch
      p = mock_func(*args, **kwargs)
  /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py:1544: in patch
      getter, attribute = _get_target(target)
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  
  target = 'hello'
  
      def _get_target(target):
          try:
              target, attribute = target.rsplit('.', 1)
          except (TypeError, ValueError):
              raise TypeError("Need a valid target to patch. You supplied: %r" %
  &gt;                           (target,))
  E           TypeError: Need a valid target to patch. You supplied: 'hello'
  
  /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py:1396: TypeError
  </code></pre>