Code

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#6515 closed (wontfix)

Django test framework requires a database

Reported by: Joe Shaw <jshaw@…> Owned by: nobody
Component: Testing framework Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

We're using Django without a database (and hence no models). We're using only views, because our web application is essentially an XML-RPC client to another server which actually contains all the data.

We'd still like to be able to use the Django's integration into unittest to test our views using django.test.client, but evidently we're required to have a database:

>>> from django.core.management import execute_manager
>>> from servweb import settings
>>> execute_manager(settings, ["manage.py", "test"])
Creating test database...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/ita/src/django/django/core/management/__init__.py", line 272, in execute_manager
    utility.execute()
  File "/ita/src/django/django/core/management/__init__.py", line 219, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/ita/src/django/django/core/management/base.py", line 72, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/ita/src/django/django/core/management/base.py", line 86, in execute
    output = self.handle(*args, **options)
  File "django/core/management/commands/test.py", line 34, in handle
    failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive)
  File "django/test/simple.py", line 142, in run_tests
    create_test_db(verbosity, autoclobber=not interactive)
  File "django/test/utils.py", line 152, in create_test_db
    cursor = connection.cursor()
  File "/ita/src/django/django/db/backends/dummy/base.py", line 14, in complain
    raise ImproperlyConfigured, "You haven't set the DATABASE_ENGINE setting yet."
django.core.exceptions.ImproperlyConfigured: You haven't set the DATABASE_ENGINE setting yet.

(Indeed, servweb.settings does not have DATABASE_ENGINE.)

We can get partial support by running the client code by hand. That is, manually setting DJANGO_SETTINGS_MODULE, importing django.test.client, and calling things like Client.get() gives us some but not all the information we'd like:

>>> r = c.get("/")
>>> print r.__dict__
{'cookies': <SimpleCookie: >, 'context': None, '_is_string': True, '_charset': 'utf-8', 
'headers': {'Content-Type': 'text/html; charset=utf-8'}, 'template': None, 'status_code': 200, '_container': [...]}

Notably, context and template are both None -- neither of them should be.

We're using 0.96.1, but I've verified this is still the case in SVN as of 2007-12-17. If you think this has been fixed more recently (a) sorry! and (b) I can update and test.

Attachments (0)

Change History (4)

comment:1 Changed 6 years ago by Joe Shaw <jshaw@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Possibly a dup of #3310?

comment:2 Changed 6 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

There is already a way to fix this problem - write a custom test runner. The default test runner (django.test.simple.run_tests) creates a test database, finds and runs the tests, and destroys the test database. It's about 15 lines of code, and fairly straight forward. However, it can be very easily replaced with a test runner that only runs the tests. Write a test runner that doesn't create a database, and you should be set.

comment:3 Changed 6 years ago by Joe Shaw <jshaw@…>

Fair enough. Can this be added to the testing documentation? I suspect this won't be the first time this is reported without docs indicating how.

comment:4 Changed 6 years ago by russellm

If you think this section can be improved, feel free to propose some additional/reworked text.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.