How to test Django-CMS plugins packaged as a reusable app
Question: I have followed the procedure in the [Django
docs](https://docs.djangoproject.com/en/dev/intro/reusable-apps/) to make some
Django-CMS plugins reusable, and the [Hitchhiker's guide to
packaging](http://guide.python-distribute.org/quickstart.html) to put them on
pypi, so they are available via

    
    
    pip install cmsplugin-rt
    

This installs them somewhere sensible that python can find them. Of course I
have my development directory somewhere else.

But when I add some tests to this package, I get the error:

    
    
    django.core.exceptions.ImproperlyConfigured: App with label cmsplugin_rt could not be found
    

Some more explanation is needed. As the package is not part of a Django
project, i.e. there is no `manage.py` file to run tests with, I followed the
advice [here at stackoverflow](http://stackoverflow.com/questions/3841725/how-
to-launch-tests-for-django-reusable-app) and added `runtests.py` to the
`tests` directory. Specifically in this file I put:

    
    
    import os, sys
    from django.conf import settings
    
    DIRNAME = os.path.dirname(__file__)
    settings.configure(DEBUG=True,
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
             }
         },
        CMS_TEMPLATES = ( ('template_for_tests.html', 'Test template'), ),
        CMS_MODERATOR = False,
        CMS_PERMISSION = False,
        TEMPLATE_CONTEXT_PROCESSORS = (
            'django.contrib.auth.context_processors.auth',
            'django.core.context_processors.i18n',
            'django.core.context_processors.request',
            'django.core.context_processors.media',
            'django.core.context_processors.static',
            'cms.context_processors.media',
            'sekizai.context_processors.sekizai',
        ),
        INSTALLED_APPS = (
            'cmsplugin_rt.button',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.admin',
            'django.contrib.sites',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            'south',
            'cms',
            'mptt',
            'menus',
            'sekizai',
        ),
    )
    
    from django.test.simple import DjangoTestSuiteRunner
    test_runner = DjangoTestSuiteRunner(verbosity=2)
    failures = test_runner.run_tests(['cmsplugin_rt', ])
    if failures:
        sys.exit(failures)
    

So, as I mentioned, when I execute `python runtests.py` I get the error:

    
    
    django.core.exceptions.ImproperlyConfigured: App with label cmsplugin_rt could not be found
    

Where am I going wrong? Or is there a better way to do this? I'd love to get
some tests in there!

Thanks!

(If this did run, would the tests run using my development version of the
package, or would they pull in the version from the pip install? Would I need
to `pip uninstall cmsplugin-rt` before each run?)

Here is my directory structure - I have several plugins in the one package,
which may be part of the problem. I put the `tests` directory as you see here,
though I have also tried it one level up.

    
    
    cmsplugin-rt/
    --README.txt
    --LICENSE.txt
    --MANIFEST.in
    --setup.py
    --cmsplugin_rt/
    ----__init__.py
    ----models.py
    ----button/
    ------__init__.py
    ------models.py
    ------cms_plugins.py
    ------templates/
    ----(other plugins)/
    ----tests/
    ------runtests.py
    ------mytests.py
    

To be safe I also put an empty `models.py` at the top level (following the
advice [here](http://stackoverflow.com/questions/6483636/how-to-test-django-
application-placed-in-subfolder)), but I'm not sure it makes any difference.


Answer: For posterity here is my work-around to the South migration problem I
mentioned in my first comment. It's not pretty, so I would love any
suggestions on how to improve it.

The process to add a new field to the `cmsplugin_rt.button` model is:

  1. Before making any edits, copy `site-packages/cmsplugin_rt/button` into a dummy Django-CMS project as an app called `button`
  2. Delete this new app's `button/migrations/` directory
  3. Add `button` to the dummy project `settings.py`'s INSTALLED_APPS
  4. Run `./manage.py schemamigration --init button`, so the dummy project's understanding of the database is aligned with the current model (before any changes are made)
  5. Run `./manage.py migrate button`, to update the dummy project's database
  6. Edit the button's `model.py` file in the dummy project to add the extra field, and make any other changes you require.
  7. Run `./manage.py schemamigration --auto button`, to generate the migration code. This will be in `button/migrations/0002_auto__...`
  8. This file is what you need to put in your package, but it will have the wrong number in the front if the plugin had more than just the `0001_initial.py` migration file in it originally. Copy it with the correct number into your package development directory. Also copy any model, cms_plugin, template and other changes you have made.

