[英]Flask blueprint unit-testing
Is there a good practice to unit-test a flask blueprint? 对烧瓶蓝图进行单元测试是否有良好的做法?
http://flask.pocoo.org/docs/testing/ http://flask.pocoo.org/docs/testing/
I didn't found something that helped me or that is simple enough. 我没有找到帮助我的东西,或者说这很简单。
// Edit //编辑
Here are my code: 这是我的代码:
# -*- coding: utf-8 -*-
import sys
import os
import unittest
import flask
sys.path = [os.path.abspath('')] + sys.path
from app import create_app
from views import bp
class SimplepagesTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app('development.py')
self.test_client = self.app.test_client()
def tearDown(self):
pass
def test_show(self):
page = self.test_client.get('/')
assert '404 Not Found' not in page.data
if __name__ == '__main__':
unittest.main()
In this case, i test the blueprint. 在这种情况下,我测试蓝图。 Not the entire app.
不是整个应用程序。 To test the blueprint i've added the root path of the app to
sys.path
. 为了测试蓝图,我已经将应用程序的根路径添加到
sys.path
。 Now i can import the create_app
function to ...create the app. 现在我可以导入
create_app
函数来创建应用程序。 I also init the test_client
. 我也初始化了
test_client
。
I think i've found a good solution. 我想我找到了一个很好的解决方案。 Or will is there a better way?
或者会有更好的方法吗?
I did the following if this helps anyone. 如果这有助于任何人,我做了以下。 I basically made the test file my Flask application
我基本上把测试文件变成了我的Flask应用程序
from flask import Flask
import unittest
app = Flask(__name__)
from blueprint_file import blueprint
app.register_blueprint(blueprint, url_prefix='')
class BluePrintTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def test_health(self):
rv = self.app.get('/blueprint_path')
print rv.data
if __name__ == '__main__':
unittest.main()
Blueprints are very similar to application. 蓝图与应用程序非常相似。 I guess that you want test
test_client
requests. 我想你想要测试
test_client
请求。
If you want test blueprint as part of your application then look like no differences there are with application. 如果您希望将测试蓝图作为应用程序的一部分,那么看起来与应用程序没有任何差异。
If you want test blueprint as extension then you can create test application with own blueprint and test it. 如果您想将测试蓝图作为扩展,那么您可以使用自己的蓝图创建测试应用程序并对其进行测试。
I have multiple APIs in one app and therefore multiple blueprints with url_prefix
. 我在一个应用程序中有多个API,因此在
url_prefix
多个蓝图。 I did not like that I have to prefix all paths when testing on of the APIs. 我不喜欢在测试API时必须为所有路径添加前缀。 I used the following class to wrap the
test_client
for blueprints: 我使用以下类来包装
test_client
以获取蓝图:
class BlueprintClient():
def __init__(self, app_client, blueprint_url_prefix):
self.app_client = app_client
self.blueprint_url_prefix = blueprint_url_prefix.strip('/')
def _delegate(self, method, path, *args, **kwargs):
app_client_function = getattr(self.app_client, method)
prefixed_path = '/%s/%s' % (self.blueprint_url_prefix, path.lstrip('/'))
return app_client_function(prefixed_path, *args, **kwargs)
def get(self, *args, **kwargs):
return self._delegate('get', *args, **kwargs)
def post(self, *args, **kwargs):
return self._delegate('post', *args, **kwargs)
def put(self, *args, **kwargs):
return self._delegate('put', *args, **kwargs)
def delete(self, *args, **kwargs):
return self._delegate('delete', *args, **kwargs)
app_client = app.test_client()
api_client = BlueprintClient(app_client, '/api/v1')
api2_client = BlueprintClient(app_client, '/api/v2')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.