python – Flask-Assets和Flask-Testing throws RegisterError:
发布时间:2020-12-20 13:29:36 所属栏目:Python 来源:网络整理
导读:我有我的Flask应用程序使用Flask-Assets并且在尝试运行unittest案例时,除了第一个测试用例之外,其他人失败并出现以下RegisterError. ======================================================================ERROR: test_login_page (tests.test_auth.AuthT
我有我的Flask应用程序使用Flask-Assets并且在尝试运行unittest案例时,除了第一个测试用例之外,其他人失败并出现以下RegisterError.
====================================================================== ERROR: test_login_page (tests.test_auth.AuthTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/cnu/env/flenv/lib/python2.7/site-packages/nose/case.py",line 133,in run self.runTest(result) File "/Users/cnu/env/flenv/lib/python2.7/site-packages/nose/case.py",line 151,in runTest test(result) File "/Users/cnu/env/flenv/lib/python2.7/site-packages/flask_testing.py",line 72,in __call__ self._pre_setup() File "/Users/cnu/env/flenv/lib/python2.7/site-packages/flask_testing.py",line 80,in _pre_setup self.app = self.create_app() File "/Users/cnu/Projects/Bookworm/App/tests/test_auth.py",line 8,in create_app return create_app('testing.cfg') File "/Users/cnu/Projects/Bookworm/App/bookworm/__init__.py",line 118,in create_app configure_extensions(app) File "/Users/cnu/Projects/Bookworm/App/bookworm/__init__.py",line 106,in configure_extensions assets.register('js_all',js) File "/Users/cnu/env/flenv/src/webassets/src/webassets/env.py",line 374,in register 'as "%s": %s' % (name,self._named_bundles[name])) RegisterError: Another bundle is already registered as "js_all": <Bundle output=assets/packed.js,filters=[<webassets.filter.jsmin.JSMin object at 0x10fa8af90>],contents=('js/app.js',)> 我理解为什么在运行第一个测试用例之前发生这种情况,create_app会创建一个app实例,并为所有其他测试用例进行维护. 我在拆解方法中尝试了del(app),但没有帮助. 有没有办法解决它? 解决方法
您可能拥有资产环境的全局对象,您已声明如下:
在文件app / extensions.py中: from flask.ext.assets import Environment assets = Environment() 然后,在create_app方法的某个地方,您应该初始化环境: 在文件app / __ init__.py中: from .extensions import assets def create_app(): app = Flask(__name__) ... assets.init_app(app) ... return app 问题是,当您使用app初始化环境时,注册的包不会被清除.因此,您应该在TestCase中手动执行此操作: 在文件测试/ __ init__.py中 from app import create_app from app.extensions import assets class TestCase(Base): def create_app(self): assets._named_bundles = {} # Clear the bundle list return create_app(self) 希望这可以帮助,干杯 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |