我在測試我的應用程序時遇到Flask-Admin創建藍圖的問題。測試期間Flask-Admin藍圖創建
這是我的看法類(使用的SQLAlchemy)
##
# All views that only admins are allowed to see should inherit from this class.
#
class AuthView(ModelView):
def is_accessible(self):
return current_user.is_admin()
class UserView(AuthView):
column_list = ('name', 'email', 'role_code')
這是我如何初始化觀點:
# flask-admin
admin.add_view(UserView(User, db.session))
admin.init_app(app)
然而,當我嘗試運行一個以上的測試(故障始終發生在第二次測試和所有其他測試後),我總是得到以下錯誤信息:
======================================================================
ERROR: test_send_email (tests.test_views.TestUser)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/lib/python2.7/site-packages/nose/case.py", line 133, in run
self.runTest(result)
File "/lib/python2.7/site-packages/nose/case.py", line 151, in runTest
test(result)
File "/lib/python2.7/site-packages/flask_testing.py", line 72, in __call__
self._pre_setup()
File "/lib/python2.7/site-packages/flask_testing.py", line 80, in _pre_setup
self.app = self.create_app()
File "/tests/test_init.py", line 27, in create_app
app = create_app(TestConfig)
File "/fbone/app.py", line 41, in create_app
configure_extensions(app)
File "/fbone/app.py", line 98, in configure_extensions
admin.add_view(UserView(User, db.session))
File "/lib/python2.7/site-packages/flask_admin/base.py", line 484, in add_view
self.app.register_blueprint(view.create_blueprint(self))
File "/lib/python2.7/site-packages/flask/app.py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/lib/python2.7/site-packages/flask/app.py", line 885, in register_blueprint
(blueprint, self.blueprints[blueprint.name], blueprint.name)
AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x110576910> and <flask.blueprints.Blueprint object at 0x1103bd3d0>. Both share the same name "userview". Blueprints that are created on the fly need unique names.
奇怪的是,這隻發生在第二次測試,從來沒有當我只是運行應用程序。
當我調試的測試中,它第一次做了什麼我的預期和init_app(應用程序)後增加的藍圖應用程序。然而,第二次過程在到達add_view步驟時立即停止(我認爲這很奇怪,因爲藍圖已在init_app(app)調用中註冊?)
當你說「第一次」你在你的第一次測試的意思還是你說你的單元測試完成後,你再次運行整個測試套裝。 – AlexLordThorsen
我的意思是第一次測試 – arnoutaertgeerts
確保在每個測試的應用程序工廠中爲您的Admin類創建新實例。看起來像不斷爲每個測試運行添加視圖到現有的'Admin'類實例。 – Joes