3

只是在一般情况下学习 python 模拟并努力使用 Magicmock 和 pytest 和 boto3。

这是我的代码块

   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)

这是不起作用的测试:

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() 

返回的错误:E AssertionError: Expected 'upload_file' to have been called。

谢谢!

4

0 回答 0