我在测试存放在子文件夹中的Django应用程序方面遇到了问题。
好吧,让我解释一下情况。
标准的Django项目结构如下:
django_project/
--appname1
--appname2
--appname3
--lib
--tests
--docs
settings.py
etc...
当项目结构是标准的时候,您只需在项目目录中输入命令,即可为应用程序 appname1 运行测试:
python2 manage.py test appname1`
我们决定把所有的应用程序放在子文件夹中,因此我们的项目结构如下所示:
django_project/
--apps/
----appname1
----appname2
----appname3
--lib
--tests
--docs
settings.py
etc...
一切都正常,但我们无法为应用程序运行测试。 我尝试了以下命令,但没有成功:
python2 manage.py test appname1
python2 manage.py test apps/appname1
python2 manage.py test apps.appname1
有没有办法使用manage.py运行子文件夹中的应用程序的测试,或者我们应该编写自己的命令来运行它们?
更新:
我们遇到了以下错误:
Traceback (most recent call last):
File "manage.py", line 18, in <module>
management.execute_manager(settings)
File "/opt/python266/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/opt/python266/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python266/lib/python2.6/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/opt/python266/lib/python2.6/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/opt/python266/lib/python2.6/site-packages/django/core/management/commands/test.py", line 37, in handle
failures = test_runner.run_tests(test_labels)
File "/opt/python266/lib/python2.6/site-packages/django/test/simple.py", line 312, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "/opt/python266/lib/python2.6/site-packages/django/test/simple.py", line 244, in build_suite
app = get_app(label)
File "/opt/python266/lib/python2.6/site-packages/django/db/models/loading.py", line 140, in get_app
raise ImproperlyConfigured("App with label %s could not be found" % app_label)
django.core.exceptions.ImproperlyConfigured: App with label appname1 could not be found
我们已经安装了应用程序设置,例如:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'project_name.apps.appname1',
'project_name.apps.appname2',
'project_name.apps.appname3',
)