1

我正在我们的烧瓶应用程序中实现烧瓶邮件,但无法摆脱应用程序耗尽上下文错误。任何帮助,将不胜感激。我已将 mail_server 参数放在配置文件中。

/app.py - 应用在 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)
...

/电子邮件.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')
4

1 回答 1

1

您应该在测试中提供应用程序上下文,例如使用app.test_request_context. 您可以在官方文档中阅读更多内容

你可以试试这个:

电子邮件.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)

测试.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. 像下面这样的东西就足够了:

应用程序.py

from flask import Flask

def create_app():
    app = Flask(__name__)
    return app

测试.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

于 2013-10-21T20:37:27.620 回答