Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#20177 closed Bug (fixed)

Don't use setup_test_environment in the shell in testing tutorial

Reported by: vlad.london.uk@… Owned by: nobody
Component: Documentation Version: 1.5
Severity: Normal Keywords:
Cc: timograham@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

I was following the instructions at https://docs.djangoproject.com/en/1.5/intro/tutorial05/

First, when executing the command in the shell:

>>> from django.test.utils import setup_test_environment

I'm getting the following error:

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

This is fixable by setting a shell environment variable

export DJANGO_SETTINGS_MODULE=mysite.settings

However, when I execute the following commands from the next section in python shell, I'm getting the results from the production database rather than from the empty test database:

>>> response = client.get(reverse('polls:index'))
>>> response.status_code
200
>>> response.content

Attachments (1)

20177.diff (2.3 KB ) - added by Tim Graham 12 years ago.

Download all attachments as: .zip

Change History (16)

comment:1 by Tim Graham, 12 years ago

How did you start the shell? If you use python manage.py shell, that should set DJANGO_SETTINGS_MODULE. We might want to add a reminder about that if it's not clear.

comment:2 by anonymous, 12 years ago

Thanks, starting the shell in this way cures the initial problem with DJANGO_SETTINGS_MODULE, but still results come from the production database rather than from the test database which is normally created anew when testing.

comment:3 by Carl Meyer, 12 years ago

Resolution: needsinfo
Status: newclosed

Did you do the second line in that first block, actually running setup_test_environment() after you import it? Can you paste a full shell session showing what you did and the results?

comment:4 by anonymous, 12 years ago

Here you go.

$ python manage.py shell
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00) 
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.test.utils import setup_test_environment
>>> from django.test.client import Client
>>> from django.core.urlresolvers import reverse
>>> setup_test_environment()
>>> client = Client()
>>> response = client.get(reverse('polls:index'))
>>> response.content
'\n\n<link rel="stylesheet" type="text/css" href="/static/polls/style.css" />\n\n\n    <ul>\n    \n        <li><a href="/polls/1/">But why?</a></li>\n    \n    </ul>\n\n'
>>>


comment:5 by Carl Meyer, 12 years ago

Resolution: needsinfo
Status: closednew
Summary: Errors in Django testing tutorialDon't use setup_test_environment in the shell in testing tutorial
Triage Stage: UnreviewedAccepted

Can you try if you run setup_test_environment() before doing from django.test.client import Client? I think the latter indirectly imports from django.db, meaning it sets up the database connection, meaning that it's too late for setup_test_environment to change the database settings.

I don't think this tutorial should be using setup_test_environment at all. It's private API, and it's fragile; it's intended for running tests, not for playing around in the shell. I think the testing tutorial should just have a note "when you run this stuff in the shell, it runs against the main database" instead. Reopening on that basis.

comment:6 by Tim Graham, 12 years ago

Actually setup_test_environment() is documented and the docs suggest:

If you want to run tests outside of ./manage.py test – for example, from a shell prompt – you will need to set up the test environment first. Django provides a convenience method to do this

They also suggest (incorrectly, as far as I can see): "This convenience method sets up the test database"

https://docs.djangoproject.com/en/dev/topics/testing/advanced/#running-tests-outside-the-test-runner

That said, I agree with Carl that we probably don't need to call setup_test_environment at all and should note that the shell commands will be run against the regular database.

by Tim Graham, 12 years ago

Attachment: 20177.diff added

comment:7 by Tim Graham, 12 years ago

Cc: timograham@… added
Has patch: set

Upon further examination, I think the setup_test_environment call needs to stay. See the patch for an explanation.

comment:8 by Tim Graham <timograham@…>, 12 years ago

Resolution: fixed
Status: newclosed

In acd9dc3888aa8af881b917028062c3bdca6e610c:

[1.5.x] Fixed #20177 - Corrected docs for django.test.utils.setup_test_environment.

Thanks vlad.london.uk@ for the report.

Backport of bc02a963db from master

comment:9 by Tim Graham <timograham@…>, 12 years ago

In bc02a963db3aeebf7c349d83a492b6e093f42b00:

Fixed #20177 - Corrected docs for django.test.utils.setup_test_environment.

Thanks vlad.london.uk@ for the report.

comment:10 by anonymous, 11 years ago

Line python manage.py shell should be added to the tutorial, because it is not obvious for the first time user. I had to end up here to progress that part.

comment:11 by anonymous, 11 years ago

You may also want to add the line

python manage.py shell

to this section of the tutorial:

https://docs.djangoproject.com/en/1.5/intro/tutorial05/#the-django-test-client

Yep, I know it was already added in https://docs.djangoproject.com/en/1.5/intro/tutorial05/#running-tests, but I also ended up here because I was going through the tutorial late at night and simply typed "python" to start the console.

Thanks in advance!

comment:12 by Christophe R. Patrouch, 11 years ago

Perhaps another line to add is one indicates that just typing Python rather than Python manage.py shell will result in errors, e.g.django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

comment:13 by anonymous, 11 years ago

Patch needs improvement: set
Resolution: fixed
Status: closednew
Triage Stage: AcceptedUnreviewed

This https://docs.djangoproject.com/en/1.6/intro/tutorial05/ needs to be fixed. It makes readers potentially loose a lot of time and getting irritated (and switching from learning how to run tests to watching youbtube).

comment:14 by Carl Meyer, 11 years ago

Patch needs improvement: unset
Resolution: fixed
Status: newclosed
Triage Stage: UnreviewedAccepted

Please don't reopen semi-related tickets, which have been correctly marked as fixed, in order to report a different issue. Adding "python manage.py shell" to that section of the tutorial may be worth doing, but it should have its own ticket.

comment:15 by Tim Graham, 11 years ago

For what it's worth, we tried to address the issue in #21027 by linking the word "shell"... perhaps it has not been entirely successful.

Note: See TracTickets for help on using tickets.
Back to Top