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

/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')
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)

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

于 2013-10-21T20:37:27.620 に答える