flask create_app和setUp单元测试 [英] flask create_app and setUp unittest
问题描述
因此,我以这种方式设置了烧瓶:
So I have setted up my flask in this way:
def create_app(config_name):
app = Flask(__name__, static_folder='../designs/UI', template_folder='../designs/UI', instance_relative_config=True)
app.config.from_object(app_config[config_name])
app.config.from_pyfile('config.py')
db.init_app(app)
login_manager.init_app(app)
login_manager.login_message = "You must be logged in to access this page."
login_manager.login_view = "auth.login_page"
migrate = Migrate(app, db)
from app import models
from .api import blueprint as api_blueprint
app.register_blueprint(api_blueprint, url_prefix='/api/v1.0')
@app.route('/')
def homepage():
return redirect('/api/v1.0')
return app
和我的单元测试设置:
def setUp(self):
"""setup test variables"""
self.app = create_app(config_name="testing")
self.client = self.app.test_client
# binds the app with the current context
with self.app.app_context():
#create all tables
db.session.close()
db.drop_all()
db.create_all()
当我将模型导入测试文件并尝试从数据库访问任何内容时,以下错误:
When I import my models into the test file and try to access anything from the database I get the following error:
RuntimeError:应用程序未在数据库实例上注册,并且没有应用程序绑定到当前上下文
RuntimeError: application not registered on db instance and no applicationbound to current context
是否存在
推荐答案
我决定在下面解释问题(和解决方案)详情。
I decided to explain the problem(and solution) in detail.
1)为什么您的方法行不通?
因为您尝试推送 app上下文
使用上下文管理器。这是什么意思? 当前上下文
仅在 with
块内可用。您绑定上下文
,并且他仅在 setUp
中可用,但在测试中不可用。 app
在 setUp()
完成后可以在没有 context
的情况下工作。让我们看这个例子:
1) Why your way doesn't work?
Because you trying to push app context
using Context Manager. What does it mean? current context
will be available only inside with
block. You bind context
and he available only in setUp
, but not in tests. app
will works without context
when setUp()
is finished. Let's look at this example:
app = Flask(__name__)
class TestCase(TestCase):
def setUp(self):
with app.app_context():
# current_app is available(you will see <Flask 'app'> in terminal)
print current_app
# out of context manager - RuntimeError: Working outside of application context.
print current_app
def test_example(self):
pass
运行测试。您会看到Flask的应用 +例外。如果删除最后一个打印(带有的
块除外),该错误将消失。
Run test. You'll be see Flask 'app' + exception. The error will disappear if you remove last print(out of with
block).
2)如何要解决此问题?
您可以在没有上下文管理器的情况下绑定 app上下文
。
class TestCase(TestCase):
def setUp(self):
ctx = app.app_context()
ctx.push()
# after this you can use current_app
print current_app
def test_example(self):
# available because we pushed context in setUp()
print current_app
运行测试:
<Flask 'app'> # from setUp()
<Flask 'app'> # from test_example
所以,总结一下。 current_app
仅在上下文管理器内部可用。但是您可以在没有上下文管理器的情况下绑定上下文。
So, let's sum up. current_app
is available ONLY 'inside' Context Manager. But you can bind context without Context Manager.
我希望我已经解释了详细信息并回答了您的问题。
I hope that I have explained the details and answered your questions.
这篇关于flask create_app和setUp单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!