#20177 closed Bug (fixed)
Don't use setup_test_environment in the shell in testing tutorial
Reported by: | 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)
Change History (16)
comment:1 by , 12 years ago
comment:2 by , 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 , 12 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
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 , 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 , 12 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
Summary: | Errors in Django testing tutorial → Don't use setup_test_environment in the shell in testing tutorial |
Triage Stage: | Unreviewed → Accepted |
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 , 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"
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 , 12 years ago
Attachment: | 20177.diff added |
---|
comment:7 by , 12 years ago
Cc: | 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 , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:10 by , 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 , 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 , 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 , 11 years ago
Patch needs improvement: | set |
---|---|
Resolution: | fixed |
Status: | closed → new |
Triage Stage: | Accepted → Unreviewed |
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 , 11 years ago
Patch needs improvement: | unset |
---|---|
Resolution: | → fixed |
Status: | new → closed |
Triage Stage: | Unreviewed → Accepted |
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 , 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.
How did you start the shell? If you use
python manage.py shell
, that should setDJANGO_SETTINGS_MODULE
. We might want to add a reminder about that if it's not clear.