Opened 2 years ago

Closed 17 months ago

Last modified 17 months 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 timo 2 years ago.

Download all attachments as: .zip

Change History (16)

comment:1 Changed 2 years ago by timo

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

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 Changed 2 years ago 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.

comment:3 Changed 2 years ago by carljm

  • Resolution set to needsinfo
  • Status changed from new to 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 Changed 2 years ago 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'
>>>


comment:5 Changed 2 years ago by carljm

  • Resolution needsinfo deleted
  • Status changed from closed to new
  • Summary changed from Errors in Django testing tutorial to Don't use setup_test_environment in the shell in testing tutorial
  • Triage Stage changed from Unreviewed to 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 Changed 2 years ago 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.

Changed 2 years ago by timo

comment:7 Changed 2 years ago by timo

  • 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 Changed 2 years ago by Tim Graham <timograham@…>

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

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 Changed 2 years ago by Tim Graham <timograham@…>

In bc02a963db3aeebf7c349d83a492b6e093f42b00:

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

Thanks vlad.london.uk@ for the report.

comment:10 Changed 2 years ago 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.

comment:11 Changed 22 months ago 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!

comment:12 Changed 20 months ago 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.

comment:13 Changed 17 months ago by anonymous

  • Patch needs improvement set
  • Resolution fixed deleted
  • Status changed from closed to new
  • Triage Stage changed from Accepted to 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 Changed 17 months ago by carljm

  • Patch needs improvement unset
  • Resolution set to fixed
  • Status changed from new to closed
  • Triage Stage changed from Unreviewed to 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 Changed 17 months ago 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.

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