开发者

How to test django application placed in subfolder?

I have problem with testing django apps grouped in subfolder.

Well, let me explain situation.

Standart django project structure looks like this:

django_project/
--appname1
--appname2
--appname3
--lib
--tests
--docs
settings.py 
etc...

When project structure is standart you can run tests for appname1 just by typing command in project dir:

python2 manage.py test appname1`

We decided to put all apps in subfoloder, so our project structure looks like this:

django_project/
--apps/
----appname1
----appname2
----appname3
--lib
--tests
--docs
settings.py 
etc...

All works fine, but we can't run tests for apps. I have tried folowing commands without success:

python2 manage.py test appname1
python2 manage.py test apps/appname1
python2 manage.py test apps.appname1

Is there any way for running tests using manage.py for apps that placed in subfolder, or we should write own command for running them?

UPD:

We have following error:

 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.buil开发者_StackOverflowd_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

We have installed apps setting like:

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',
 )


You need both these files under your app folder:

__init__.py
models.py

They can be empty.


What error do you get? And what do you have under your INSTALLED_APPS in settings.py?

if you have something like

INSTALLED_APPS = (
    'django.contrib.auth',
    ...
    'apps.appname1',
    'apps.appname2',
)

and __init__.py in the apps directory then it should work.


Just a note, the settings.py file contains a tuple so the termination character should be ')' not '}'


In django 2.2.4 , using a folder for test files i.e. \test , instead of the default test.py file. This is what worked for me.

python manage.py test .\apps-folder\app1

If you are also using a folder for test files, make sure include a __init__.py


Make sure apps has an __init__.py. You should be able to run your tests by app name:

python2 manage.py test appname1

This works under Django 1.3, I haven't tested any other versions.


This is a bit late as an answer, but for future reference of other peeps coming across this I was running into the same problem, and found that I needed to add an empty models.py file to the app. (It didn't need any database tables, but not having a models.py meant that the test runner was not picking it up.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜