Flask-Assets and Flask-Testing throws RegisterError: Another bundle is already registered

670 views Asked by At

I have my Flask app which uses Flask-Assets and while trying to run the unittest cases, except the first testcase, others fails with the following 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',)>

My understanding of why this happens in before the first testcase is run, create_app creates an instance of app and this is maintained for all other testcases.

I tried del(app) in the teardown method, but doesn't help.

Is there some way to fix it?

1

There are 1 answers

0
Ghis On

You probably have a global object for the assets environment, which you have declared as such :

in file app/extensions.py:

from flask.ext.assets import Environment
assets = Environment()

Then, somewhere in your create_app method, you should init the environment:

in file app/__init__.py:

from .extensions import assets

def create_app(): 
    app = Flask(__name__)
    ...
    assets.init_app(app)
    ...
    return app

The thing is that when you initialize your environment with you app, the registered bundles aren't cleared. So you you should do it manually as such in your TestCase:

in file 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)

Hope this helps, Cheers