LoginSignup
2
1

More than 5 years have passed since last update.

moto を使って boto3 と Amazon SES を利用したスクリプトのテストコードを書く方法

Posted at

moto という AWS サービス向けのモックツールがあり、SES を利用したスクリプトのテストも行えたので、その手順を共有します。

手元の環境は以下になります。

  • Ubuntu 16.04 LTS
  • Python 3.6.5 :: Anaconda, Inc.
  • boto3==1.7.42 、moto==1.3.3

注意点としては、SES のクライアントを生成した後に verify_email_identity する必要があったことです。

import unittest

import boto3

from moto import mock_ses

from send_email_to_me import aws_util

class TestAwsUtil(unittest.TestCase):
    @mock_ses
    def test_send_mail(self):
        ses = boto3.client('ses', region_name='us-east-1')
        ses.verify_email_identity(EmailAddress='test@example.com')
        data = open('tests/test.jpg', 'rb')
        attach_file = data.read()
        attach_file_name = 'test.jpg'
        data.close()
        msg = aws_util.make_mime('test@example.com',
                                 'test_subject',
                                 'test_body',
                                 attach_file,
                                 attach_file_name)
        responses = aws_util.send_email(ses,
                                        'test@example.com',
                                        'test_to@example.com',
                                        msg)
        self.assertEqual(responses[0]['ResponseMetadata']['HTTPStatusCode'], 200)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1