title: Pytest mocker patch - how to troubleshoot?
body: <p>I am having what I believe to be a common issue in using mock patching in that I can not figure out the <em>right thing</em> to patch.  </p>
  
  <p>I have two questions that I am hoping for help with.</p>
  
  <ol>
  <li>Thoughts on how to fix the specific issue in the below example</li>
  <li>And possibly most-importantly pro-tips/pointers/thoughts/suggestions on how to best troubleshoot the "which thing do I patch" question.  The problem I'm having is, without a full understanding of how patching works, I really dont even know what I should be looking for and find myself playing a guessing game.</li>
  </ol>
  
  <p>An example using <code>pyarrow</code> that is currently causing me pain:</p>
  
  <h3>mymodule.py</h3>
  
  <pre><code>import pyarrow
  
  class HdfsSearch:
      def __init__(self):
          self.fs = self._connect()
  
      def _connect(self) -&gt; object:
          return pyarrow.hdfs.connect(driver="libhdfs")
  
      def search(self, path: str):
          return self.fs.ls(path=path)
  </code></pre>
  
  <h3>test_module.py</h3>
  
  <pre><code>import pyarrow
  import pytest
  
  from mymodule import HdfsSearch
  
  @pytest.fixture()
  def hdfs_connection_fixture(mocker):
      mocker.patch("pyarrow.hdfs.connect")
      yield HdfsSearch()
  
  def test_hdfs_connection(hdfs_connection_fixture):
      pyarrow.hdfs.connect.assert_called_once() # &lt;-- succeeds
  
  def test_hdfs_search(hdfs_connection_fixture):
      hdfs_connection_fixture.search(".")
      pyarrow.hdfs.HadoopFileSystem.ls.assert_called_once() # &lt;-- fails
  </code></pre>
  
  <h3>pytest output:</h3>
  
  <pre><code>$ python -m pytest --verbose test_module.py
  =========================================================================================================== test session starts ============================================================================================================
  platform linux -- Python 3.7.4, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- /home/bbaur/miniconda3/envs/dev/bin/python
  cachedir: .pytest_cache
  rootdir: /home/user1/work/app
  plugins: cov-2.7.1, mock-1.10.4
  collected 2 items
  
  test_module.py::test_hdfs_connection PASSED                                                                                                                                                                                          [ 50%]
  test_module.py::test_hdfs_search FAILED                                                                                                                                                                                              [100%]
  
  ================================================================================================================= FAILURES =================================================================================================================
  _____________________________________________________________________________________________________________ test_hdfs_search _____________________________________________________________________________________________________________
  
  hdfs_connection_fixture = &lt;mymodule.HdfsSearch object at 0x7fdb4ec2a610&gt;
  
      def test_hdfs_search(hdfs_connection_fixture):
          hdfs_connection_fixture.search(".")
  &gt;       pyarrow.hdfs.HadoopFileSystem.ls.assert_called_once()
  E       AttributeError: 'function' object has no attribute 'assert_called_once'
  
  test_module.py:16: AttributeError
  </code></pre>