Django Tutorial 5 - First Tests

185 views
Skip to first unread message

Paul Sermon

unread,
Feb 12, 2014, 4:33:21 PM2/12/14
to django...@googlegroups.com
So I'm working my way through the Django tutorials, and have got as far as the view tests.  It seems that when running the test client, rather than returning data from an empty test database (is this what is meant to happen?) it is returning polls from the existing database, which has the polls created in the previous tutorials.  This means the tests that expect empty result sets fail.

Any idea why it would be doing this?  Am I correct in thinking the test should create it's own empty database to test this?

Jonathan Baker

unread,
Feb 12, 2014, 5:33:16 PM2/12/14
to django...@googlegroups.com
You're correct in thinking that tests use a test database that is independent of your formally defined project database. Without knowing exactly which Django tutorials you are referring to, I can only assume that perhaps you:

1) Have defined initial_data.json fixtures
2) Have defined other fixtures that your tests are referencing (likely in their setUp method)

If none of the items above are correct, feel free to provide a link to the tutorial you're going through and I'll take a look.

JDB


On Wed, Feb 12, 2014 at 2:33 PM, Paul Sermon <pauls...@googlemail.com> wrote:
So I'm working my way through the Django tutorials, and have got as far as the view tests.  It seems that when running the test client, rather than returning data from an empty test database (is this what is meant to happen?) it is returning polls from the existing database, which has the polls created in the previous tutorials.  This means the tests that expect empty result sets fail.

Any idea why it would be doing this?  Am I correct in thinking the test should create it's own empty database to test this?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e1782c5f-d8e7-4fc6-93d8-88d01aafbd4f%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Paul Sermon

unread,
Feb 13, 2014, 5:19:39 AM2/13/14
to django...@googlegroups.com
This is the tutorial: https://docs.djangoproject.com/en/1.6/intro/tutorial05/

I've followed the tutorials pretty much to the letter since tutorial one.

Thanks for the help!

-Paul

Jonathan Baker

unread,
Feb 13, 2014, 9:58:37 AM2/13/14
to django...@googlegroups.com
Are you running your tests on the command line using:

$ python manage.py test polls

...or are you trying to run them from a Python shell? It'd be helpful if you could provide the internal link to the area of the page you linked to that you're experiencing the failure at, as well as the output of the error you're receiving and the command that causes it.

JDB



For more options, visit https://groups.google.com/groups/opt_out.



--
Jonathan D. Baker
Developer
http://jonathandbaker.com

Paul Sermon

unread,
Feb 13, 2014, 12:20:20 PM2/13/14
to django...@googlegroups.com
yes I am running through the manage.py shell (although initially I did not).  

On the response.content line, I get back a line listing the polls made earlier.

And I presume that is why the test from the next section fail, because they are picking up the entries from the database, rather than the test database.

I am on windows 7, if that makes any difference.

Jonathan Baker

unread,
Feb 13, 2014, 12:29:50 PM2/13/14
to django...@googlegroups.com
"setup_test_environment() installs a template renderer which will allow us to examine some additional attributes on responses such as response.context that otherwise wouldn’t be available. Note that this method does not setup a test database, so the following will be run against the existing database and the output may differ slightly depending on what polls you already created."

That's from the section you linked to, and it clearly explains that a test database will not be created. Two sections below that is https://docs.djangoproject.com/en/1.6/intro/tutorial05/#testing-our-new-view which demonstrates how to create test cases that inherit from TestCase that can be found and executed by the Django test runner which will also handle creating the test database for you.

JDB



For more options, visit https://groups.google.com/groups/opt_out.

Paul Sermon

unread,
Feb 13, 2014, 1:17:20 PM2/13/14
to django...@googlegroups.com
Hmm, you'r right, I think I'm getting confused and attributed the error to something it isn't.  Ive just ran the test after changing all the polls to the future, and I still get the errors.  The errors are as follows:

======================================================================
FAIL: test_index_view_with_a_future_poll (polls.tests.PollViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Alienware\Dropbox\coding stuff\web\django\mysite\polls\tests.py", line 75, in test_index_view_with_a_fu
ture_poll
    self.assertContains(response, "No polls are available.", status_code=200)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 351, in assertContains
    msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: Couldn't find 'No polls are available.' in response

======================================================================
FAIL: test_index_view_with_no_polls (polls.tests.PollViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Alienware\Dropbox\coding stuff\web\django\mysite\polls\tests.py", line 54, in test_index_view_with_no_p
olls
    self.assertContains(response, "No polls are available.")
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 351, in assertContains
    msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: Couldn't find 'No polls are available.' in response

----------------------------------------------------------------------
Ran 8 tests in 0.037s

FAILED (failures=2)
Destroying test database for alias 'default'...

C. Kirby

unread,
Feb 13, 2014, 1:22:53 PM2/13/14
to django...@googlegroups.com
The names of both those tests imply that your setup will fail them. (I didn't look at the test internals, so I am assuming the test names do what they say)
1. "test_index_view_with_a_future_poll" - you have future polls
2. "test_index_view_with_no_polls" - you have polls in the database

Jonathan Baker

unread,
Feb 13, 2014, 1:30:34 PM2/13/14
to django...@googlegroups.com
Cool, so we're making progress and you're using the test database now. Since you have two tests failing, I'd comment out the 2nd of the two for now and focus on getting the first to pass, and then moving on to the second. Don't try to do too much at once, and do try and understand what each function/method is doing.

Firstly, I'm not a fan of assertions like:
self.assertContains(response, "No polls are available.", status_code=200)
...because it's testing two things at once. I'd break that out in to two separate statements:
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No polls are available.")

Run the tests again, and since it's likely still failing at the assertContains point, I'd comment that out and print out the response and inspect it manually to see what it actually contains. Perhaps there is a typo in your 'No polls are available' string, or perhaps the view is actually returning queryset results. Dig around a bit. Getting comfortable with tests and good at writing them will give you a deeper understanding of the framework than simply writing views, models and templates ever wil.

JDB



For more options, visit https://groups.google.com/groups/opt_out.

Paul Sermon

unread,
Feb 13, 2014, 3:54:30 PM2/13/14
to django...@googlegroups.com
OK thanks for your help.

I've reviewed through the code, and I had a minor indentation typo that wasn't helping!

Anyway, I worked it down to the lines self.assertContains.  You see that full-stop/period at the end of the string it was looking for?  That wasn't in the no polls message!  That'll teach me for copying and pasting!

Thanks for your help!

-Paul

Jonathan Baker

unread,
Feb 13, 2014, 4:48:11 PM2/13/14
to django...@googlegroups.com
Glad you worked it out. Enjoy the rest of the tutorial!

JDB


Reply all
Reply to author
Forward
0 new messages