我是python和app引擎的新手。
我有这个代码,在一些auth逻辑之后根据请求参数发送一封电子邮件。在我的单元测试中(我正在使用GAEUnit),如何确认发送了具有特定内容的电子邮件? - 即如何使用虚假的电子邮件模拟电子邮件以验证发送是否被调用?
class EmailHandler(webapp.RequestHandler):
def bad_input(self):
self.response.set_status(400)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write("<html><body>bad input </body></html>")
def get(self):
to_addr = self.request.get("to")
subj = self.request.get("subject")
msg = self.request.get("body")
if not mail.is_email_valid(to_addr):
# Return an error message...
# self.bad_input()
pass
# authenticate here
message = mail.EmailMessage()
message.sender = "my.company@gmail.com"
message.to = to_addr
message.subject = subj
message.body = msg
message.send()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write("<html><body>success!</body></html>")
单元测试,
import unittest
from webtest import TestApp
from google.appengine.ext import webapp
from email import EmailHandler
class SendingEmails(unittest.TestCase):
def setUp(self):
self.application = webapp.WSGIApplication([('/', EmailHandler)], debug=True)
def test_success(self):
app = TestApp(self.application)
response = app.get('http://localhost:8080/send?to=vijay.santhanam@gmail.com&body=blah_blah_blah&subject=mySubject')
self.assertEqual('200 OK', response.status)
self.assertTrue('success' in response)
# somehow, assert email was sent
答案 0 :(得分:3)
您还可以覆盖AppEngine中_GenerateLog
内的mail_stub
方法。
以下是我在测试发送电子邮件时用作mixin的父TestCase类:
from google.appengine.api import apiproxy_stub_map, mail_stub
__all__ = ['MailTestCase']
class MailTestCase(object):
def setUp(self):
super(MailTestCase, self).setUp()
self.set_mail_stub()
self.clear_sent_messages()
def set_mail_stub(self):
test_case = self
class MailStub(mail_stub.MailServiceStub):
def _GenerateLog(self, method, message, log, *args, **kwargs):
test_case._sent_messages.append(message)
return super(MailStub, self)._GenerateLog(method, message, log, *args, **kwargs)
if 'mail' in apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map:
del apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map['mail']
apiproxy_stub_map.apiproxy.RegisterStub('mail', MailStub())
def clear_sent_messages(self):
self._sent_messages = []
def get_sent_messages(self):
return self._sent_messages
def assertEmailSent(self, to=None, sender=None, subject=None, body=None):
for message in self.get_sent_messages():
if to and to not in message.to_list(): continue
if sender and sender != message.sender(): continue
if subject and subject != message.subject(): continue
if body and body not in message.textbody(): continue
return
failure_message = "Expected e-mail message sent."
args = []
if to: args.append('To: %s' % to)
if sender: args.append('From: %s' % sender)
if subject: args.append('Subject: %s' % subject)
if body: args.append('Body (contains): %s' % body)
if args:
failure_message += ' Arguments expected: ' + ', '.join(args)
self.fail(failure_message)
之后,示例测试用例可能如下所示:
import unittest, MailTestCase
class MyTestCase(unittest.TestCase, MailTestCase):
def test_email_sent(self):
send_email_to('test@example.org') # Some method that would send an e-mail.
self.assertEmailSent(to='test@example.org')
self.assertEqual(len(self.get_sent_messages()), 1)
答案 1 :(得分:2)
非常简短的介绍提供了PyPI: MiniMock 1.0。这是一个非常小的库来建立模拟。
答案 2 :(得分:2)
只需使用以下内容即可获取自激活邮件存根以来发送的所有邮件。
from google.appengine.api import apiproxy_stub_map
sent_messages = apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map['mail'].get_sent_messages()
答案 3 :(得分:1)
我在GAEUnit中使用了jgeewax的GAE Testbed。使用GAEUnit代替NoseGAE对我来说更容易,因为我已经有了GAEUnit。以下是步骤:
将GAEUnit添加到您的应用
gaeunit.py
复制到应用的根文件夹。app.yaml
,直接位于handlers:
行的下方:代码:
- url: /test.*
script: gaeunit.py
(可选)从存档中提取的文件夹中,有一个名为sample_app
的文件夹,里面是webtest
模块的修改版本。将webtest
模块(包含debugapp.py
和__init__.py
的整个文件夹)复制到您应用的根目录。
将GAE Testbed添加到GAEUnit:
gaetestbed
模块(它是名为“gaetestbed”的文件夹)。将模块复制到应用程序的根目录。test_mailer.py
。test_mailer.py
:代码:
import unittest
from gaetestbed import MailTestCase
class MyTestCase(MailTestCase, unittest.TestCase):
def test_email_sent(self):
send_email_to('test@example.org') # Some method that sends e-mail...
self.assertEmailSent(to='test@example.org')
self.assertEqual(len(self.get_sent_messages()), 1)
启动您的服务器并转到http://localhost:8080/test
。您应该注意到http://localhost:8080/test
已经运行了(额外的)1/1测试。
来源:Using GAE Testbed with GAEUnit: Testing that email was sent