我有我的Flask应用程序,它使用Flask-Assets,在尝试运行单元测试用例时,除了第一个测试用例之外,其他应用程序都失败了,并显示以下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实例,并为所有其他测试用例维护此实例。
我在teardown方法中尝试了del(app),但没有帮助。
有什么办法可以解决这个问题吗?
发布于 2014-01-06 18:35:40
您可能有一个用于assets环境的全局对象,您已将其声明为:
在文件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问题是,当你用你的应用程序初始化你的环境时,注册的捆绑包没有被清除。因此,您应该在您的TestCase中手动执行此操作:
在文件tests/__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)希望这能帮上忙,干杯
https://stackoverflow.com/questions/11981187
复制相似问题