3

just learning python mocking in general and struggling with using Magicmock and pytest with boto3.

Here is my code block

   def upload_to_s3(self, local_file, bucket, dest_file):

        self.local_file = local_file
        self.bucket = bucket
        self.dest_file = dest_file

        s3_client = self.prime_s3_client() # this method returns the boto3 client
        try:
            s3_client.upload_file(local_file, bucket, dest_file)
            LOG_IT.info('File uploaded to S3 from: %s to %s.', local_file, dest_file)
        except Exception:
            LOG_IT.critical('The %s failed to upload to S3.', local_file)

This is the test that's not working:

def test_upload_to_s3(self, monkeypatch, aws):
        mock_s3_client = MagicMock()
        monkeypatch.setattr(boto3, 'client', mock_s3_client)
        mock_upload_file = MagicMock()
        monkeypatch.setattr(mock_s3_client, 'upload_file', mock_upload_file)
        push_to_s3 = aws.upload_to_s3('localfile', 'chumbucket', 'destfile')
        mock_upload_file.assert_called() 

The error returned: E AssertionError: Expected 'upload_file' to have been called.

Thank you!

tdw_8
  • 31
  • 2

0 Answers0