0
我正在嘗試使用pytest爲Flask應用程序編寫單元測試。我有一個應用程序的工廠:爲Flask測試客戶端生成URL
def create_app():
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
import os
app.secret_key = os.urandom(24)
from models import db
db.init_app(app)
return app
和測試類:
class TestViews(object):
@classmethod
def setup_class(cls):
cls.app = create_app()
cls.app.testing = True
cls.client = cls.app.test_client()
@classmethod
def teardown_class(cls):
cls.app_context.pop()
def test_create_user(self):
"""
Tests the creation of a new user.
"""
view = TestViews.client.get(url_for('create_users')).status_code == 200
但是當我運行我的測試中,我得到以下錯誤:
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
谷歌搜索這告訴我(我認爲)使用測試客戶端應創建一個自動應用程序上下文。我錯過了什麼?