0

I'm trying to test a Django reusable app that was organized as Python packages. Here is the directory tree:

reusableapp
├── __init__.py
└── submodule
    ├── __init__.py
    └── app
        ├── __init__.py
        ├── models.py
        ├── tests.py
        └── views.py

My version of Django is 1.5. To pick the application for testing I have the following code based on the one exposed here:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Description: Execute tests from outside Django's project
"""


import os
import sys

from django.conf import settings


DIRNAME = os.path.dirname(__file__)

INSTALLED_APPS = [
    "reusableapp.submodule.app"
]

settings.configure(
    DEBUG=True,
    DATABASES={
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
        }
    },
    INSTALLED_APPS=tuple(INSTALLED_APPS),
    CACHES={
        "default": {
            "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
        }
    },
)


if __name__ == "__main__":

    from django.test.simple import DjangoTestSuiteRunner

    test_runner = DjangoTestSuiteRunner(verbosity=1)
    failures = test_runner.run_tests(INSTALLED_APPS)
    if failures:
        sys.exit(failures)

But when I execute it I got the following error (using virtualenv):

(reusableapp) $ python runtests.py
Traceback (most recent call last):
  File "runtests.py", line 48, in <module>
    failures = test_runner.run_tests(INSTALLED_APPS)
  File "/var/lib/virtualenvs/reusableapp/local/lib/python2.7/site-packages/django/test/simple.py", line 369, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/var/lib/virtualenvs/reusableapp/local/lib/python2.7/site-packages/django/test/simple.py", line 254, in build_suite
    suite.addTest(build_test(label))
  File "/var/lib/virtualenvs/reusableapp/local/lib/python2.7/site-packages/django/test/simple.py", line 102, in build_test
    app_module = get_app(parts[0])
  File "/var/lib/virtualenvs/reusableapp/local/lib/python2.7/site-packages/django/db/models/loading.py", line 160, in get_app
    raise ImproperlyConfigured("App with label %s could not be found" % app_label)
django.core.exceptions.ImproperlyConfigured: App with label reusableapp could not be found

I've searched in the Django documentation about reusable apps and formats but it says nothing beyond "just a Python package that is specifically intended for use in a Django project. An app may also use common Django conventions, such as having a models.py file"

Do you know some explicit convention/requeriment that explicits the reusable app format? If not, have you faced this situation? Is there a way to load the app by force?

Thank you and best regards.

1 Answer 1

0

Lets say your app path is /parentdir/reusableapp/submodule/app

First make sure you have /parentdir/ in your PYTHONPATH by running following command in python interpreter :-

>>> import os
>>> os.environ['PYTHONPATH'].split(os.pathsep)

Probably you will not see this path, add following line in your django project manage.py file.

sys.path.append('/parentdir/')

Also -
Did you check setting django.template.loaders.app_directories.Loader in TEMPLATE_LOADERS, If it's not already there try to add it like this -

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

django document on template subdirectories - https://docs.djangoproject.com/en/dev/ref/templates/api/#using-subdirectories

Hope it will help.

Sign up to request clarification or add additional context in comments.

1 Comment

This could be used if I am inside a Django project. What I have is the source code of a reusable app outside a project using that snippet to execute the test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.