我正在我们的烧瓶应用程序中实现烧瓶邮件,但无法摆脱应用程序用完的上下文错误。任何帮助,将不胜感激。我已将mail_server params放在配置文件中。
/app.py - app在create_web_apis
中实例化def create_app(config):
"""Creates an instance of the app according to `config`
:param config: An instance of :class:`flask.config.Config`
:returns: The configured application. This can be passed to a WSGI
container.
"""
app = create_web_apis()
app.config.update(config)
mail = Mail(app)
...
/emails.py
from flask import current_app
from flask.ext.mail import Mail, Message
mail = Mail(current_app)
def send_email(subject, sender, recipients, text_body, html_body):
msg = Message(subject, sender = sender, recipients = recipients)
msg.body = text_body
msg.html = html_body
mail.send(msg)
这里是完整的跟踪
Traceback (most recent call last):
File "bin/run-tests", line 58, in <module>
test_suite = test_loader.loadTestsFromNames(args.tests)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName
module = __import__('.'.join(parts_copy))
File "/Users/tahsin/dev/restful_phollow/phollow/tests/test_external.py", line 16, in <module>
from phollow.emails import send_email
File "/Users/tahsin/dev/restful_phollow/phollow/emails.py", line 6, in <module>
mail = Mail(current_app)
File "/Users/tahsin/dev/venv/phollow/lib/python2.7/site-packages/flask_mail.py", line 461, in __init__
self.state = self.init_app(app)
File "/Users/tahsin/dev/venv/phollow/lib/python2.7/site-packages/flask_mail.py", line 473, in init_app
app.config.get('MAIL_SERVER', '127.0.0.1'),
File "/Users/tahsin/dev/venv/phollow/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "/Users/tahsin/dev/venv/phollow/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
return self.__local()
File "/Users/tahsin/dev/venv/phollow/lib/python2.7/site-packages/flask/globals.py", line 26, in _find_app
raise RuntimeError('working outside of application context')
答案 0 :(得分:1)
您应该在测试中提供应用程序上下文,例如使用app.test_request_context
。您可以在official docs
你可以试试这个:
emails.py
from flask import current_app
from flask.ext.mail import Mail, Message
def send_email(subject, sender, recipients, text_body, html_body):
mail = Mail(current_app)
msg = Message(subject, sender = sender, recipients = recipients)
msg.body = text_body
msg.html = html_body
mail.send(msg)
test.py
import unittest
from app import create_app
from flask.ext.mail import Message
from emails import send_email
class AppTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app()
def test_mail(self):
with self.app.test_request_context('/'):
send_email("Hello",
sender="from@example.com",
recipients=["to@example.com"],
text_body='',
html_body='',
)
if __name__ == '__main__':
unittest.main()
注意强>:
如果您在Mail
中创建app.py
个实例,请不要在emails.py
中重复。某物
如下所示应该足够了:
app.py
from flask import Flask
def create_app():
app = Flask(__name__)
return app
test.py
import unittest
from app import create_app
from flask.ext.mail import Message
class AppTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app()
def test_mail(self):
with self.app.test_request_context('/'):
self.app.mail.send(Message("Hello",
sender="from@example.com",
recipients=["to@example.com"]))
if __name__ == '__main__':
unittest.main()
您可以使用DebuggingServer
进行测试 python -m smtpd -n -c DebuggingServer localhost:1025