[Django] #20177: Errors in Django testing tutorial

21 views
Skip to first unread message

Django

unread,
Apr 1, 2013, 12:59:48 PM4/1/13
to django-...@googlegroups.com
#20177: Errors in Django testing tutorial
----------------------------------+--------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------
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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20177>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 2, 2013, 7:10:24 AM4/2/13
to django-...@googlegroups.com
#20177: Errors in Django testing tutorial
----------------------------------+--------------------------------------

Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:1>

Django

unread,
Apr 2, 2013, 8:06:15 AM4/2/13
to django-...@googlegroups.com
#20177: Errors in Django testing tutorial
----------------------------------+--------------------------------------

Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by anonymous):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:2>

Django

unread,
Apr 2, 2013, 10:36:12 AM4/2/13
to django-...@googlegroups.com
#20177: Errors in Django testing tutorial
----------------------------------+--------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by carljm):

* status: new => closed
* resolution: => needsinfo


Comment:

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?

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:3>

Django

unread,
Apr 2, 2013, 10:53:35 AM4/2/13
to django-...@googlegroups.com
#20177: Errors in Django testing tutorial
----------------------------------+--------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by anonymous):

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'
>>>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:4>

Django

unread,
Apr 2, 2013, 11:04:40 AM4/2/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------

Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by carljm):

* status: closed => new
* resolution: needsinfo =>
* stage: Unreviewed => Accepted


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:5>

Django

unread,
Apr 2, 2013, 8:34:10 PM4/2/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by timo):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:6>

Django

unread,
Apr 4, 2013, 7:01:57 AM4/4/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by timo):

* cc: timograham@… (added)
* has_patch: 0 => 1


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:7>

Django

unread,
May 6, 2013, 1:57:03 PM5/6/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed

* resolution: => fixed


Comment:

In [changeset:"acd9dc3888aa8af881b917028062c3bdca6e610c"]:
{{{
#!CommitTicketReference repository=""
revision="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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:8>

Django

unread,
May 6, 2013, 2:03:52 PM5/6/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"bc02a963db3aeebf7c349d83a492b6e093f42b00"]:
{{{
#!CommitTicketReference repository=""
revision="bc02a963db3aeebf7c349d83a492b6e093f42b00"


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

Thanks vlad.london.uk@ for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:9>

Django

unread,
Jun 30, 2013, 5:29:03 PM6/30/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by anonymous):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:10>

Django

unread,
Sep 9, 2013, 12:20:28 PM9/9/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by anonymous):

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!

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:11>

Django

unread,
Nov 21, 2013, 5:21:05 PM11/21/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by Christophe R. Patrouch):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:12>

Django

unread,
Nov 21, 2013, 5:28:32 PM11/21/13
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by xpatmx):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:13>

Django

unread,
Jan 27, 2014, 7:21:54 PM1/27/14
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+--------------------------------------

Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by anonymous):

* status: closed => new

* needs_better_patch: 0 => 1
* resolution: fixed =>
* stage: Accepted => Unreviewed


Comment:

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).

Django

unread,
Jan 27, 2014, 7:28:06 PM1/27/14
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by carljm):

* status: new => closed

* needs_better_patch: 1 => 0
* resolution: => fixed


* stage: Unreviewed => Accepted


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:14>

Django

unread,
Jan 27, 2014, 7:30:34 PM1/27/14
to django-...@googlegroups.com
#20177: Don't use setup_test_environment in the shell in testing tutorial
----------------------------------+------------------------------------
Reporter: vlad.london.uk@… | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 1.5

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by timo):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:15>

Reply all
Reply to author
Forward
0 new messages