vendredi 23 mars 2018

Test an SFTP Connection in Python

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.

I have a method:

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

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.

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?

Aucun commentaire:

Enregistrer un commentaire