2012-08-16 49 views
4

我有我的Flask应用程序,它使用Flask-Assets,并试图运行unittest的情况下,除了第一个测试用例,其他的失败以及下面的RegisterError。Flask-Assets和Flask-Testing引发RegisterError:另一个bundle已经注册

====================================================================== 
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创建应用程序的实例,这是维护所有其他测试用例。

我在拆卸方法中试过del(app),但没有帮助。

有什么方法可以解决它吗?

回答

1

你可能有资产的环境,你已经宣布为一个全局对象:

文件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 

问题是,当你用你的应用程序初始化你的环境时,注册的包不会被清除。所以,你应该做手工,因为这样在你的测试用例:

文件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) 

希望这有助于 干杯

相关问题