title: Test an SFTP Connection in Python
body: <p>I previously asked a question about mocks and testing and it helped me to get all but one aspect of testing sorted. How to test an SFTP connection.</p>
  
  <p>I have a method:</p>
  
  <pre><code>def _transfer_file(self, my_file):
    try:
      with pysftp.Connection(‘host’, username=username, password=password) as sftp:
        sftp.put(my_file)
        sftp.close()
        return True
  
     except Exception as e:
        # log an error with exception
        return None
  </code></pre>
  
  <p>My issue is how do I test that specific method? I think it would be sufficient to simple verify that the connection can be successfully opened but how do I do this? My test would end up sending “my_file” which I do not really want to do.</p>
  
  <p>I suspect I will need to use @mock.patch(something) to fake a connection? I’m not really sure. Does anyone have any suggestions on how to test this situation?</p>