我正在尝试测试一个组织为Python包的Django可重用应用程序。这是目录树:
reusableapp
├── __init__.py
└── submodule
├── __init__.py
└── app
├── __init__.py
├── models.py
├── tests.py
└── views.py
我的Django版本是1.5。要选择要测试的应用程序,我有以下代码,基于暴露的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)
但是当我执行它时,我得到了以下错误(使用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
我在Django文档中搜索了可重用的应用程序和格式,但除了"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"之外没有其他内容
你知道一些明确的约定/ requeriment,它可以解释可重用的应用程序格式吗?如果没有,你有没有遇到过这种情况?有没有办法强行加载应用程序?
谢谢你,并致以最诚挚的问候。
答案 0 :(得分:0)
让我们说您的应用路径是/ parentdir / reusableapp / submodule / app
首先通过在python解释器中运行以下命令,确保你的PYTHONPATH中有/ parentdir /: -
>>> import os
>>> os.environ['PYTHONPATH'].split(os.pathsep)
您可能不会看到此路径,请在django项目manage.py文件中添加以下行。
sys.path.append('/parentdir/')
另外 -
您是否检查过在TEMPLATE_LOADERS中设置django.template.loaders.app_directories.Loader,如果还没有,请尝试像这样添加它 -
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
关于模板子目录的django文档 - https://docs.djangoproject.com/en/dev/ref/templates/api/#using-subdirectories
希望它会有所帮助。