Permalink
Browse files

update tutorials to latest.

  • Loading branch information...
1 parent bc41596 commit 230fba8e7b40cebb67ea08bc1ec4bc0da254f058 @hjwp committed Sep 28, 2012
Showing with 45 additions and 42 deletions.
  1. +14 −14 tutorial01.rst
  2. +5 −5 tutorial02.rst
  3. +11 −8 tutorial03.rst
  4. +2 −2 tutorial04.rst
  5. +13 −13 tutorial05.rst
View
@@ -36,8 +36,8 @@ So here are our objectives for this first tutorial:
========================================= ==================================
Objective How will we know we've succeeded?
========================================= ==================================
-Set up Django Run the *Django test server* and
- manually browse the default
+Set up Django Run the *Django development server*
+ and manually browse the default
"Hello World" page
----------------------------------------- ----------------------------------
Set up the Django admin site Write our first *functional test*,
@@ -109,10 +109,10 @@ This tutorial is written for 1.4 -- bleeding edge FTW! -- but if you're stuck
with an earlier version, you should find most things work with a little
tweaking, as long as you get yourself a hold of a LiveServerTestCase backport)*
-Checking we've succeeded: running the test server
--------------------------------------------------
+Checking we've succeeded: running the development server
+--------------------------------------------------------
-Django comes with a built-in test server which you can fire up during
+Django comes with a built-in development server which you can fire up during
development to take a peek at how things are looking. You can start it up by
typing::
@@ -124,7 +124,7 @@ should see something a bit like this:
.. image:: /static/images/django_it_worked_default_page.png
-There's more information about the test server here:
+There's more information about the development server here:
https://docs.djangoproject.com/en/1.4/intro/tutorial01/#the-development-server
Now, manual tests like the one we've just done are all very well, but in TDD
@@ -133,7 +133,7 @@ automated test.
I did want to introduce ``runserver`` at the outset though - that way, at any
point during this tutorial, if you want to check what the site actually looks
-like, you can always fire up the test server and have a look around
+like, you can always fire up the development server and have a look around
Starting our first functional test: The Django admin site
@@ -213,7 +213,7 @@ Your directory tree will now look like this::
`-- wsgi.py
-Now, let's open up the ``tests.py`` file inside the ``fts`` folder called, and
+Now, let's open up the ``fts/tests.py`` file (inside the ``fts`` folder), and
write our first Functional test. You can delete the example test case that
Django have put in there, and replace it with this:
@@ -556,7 +556,7 @@ take for our site.::
python manage.py runserver
If you take another look at ``http://localhost/``, you will probably see an
-error message like this::
+error message like this:
.. image:: /static/images/page_not_found_debug_error.png
@@ -586,7 +586,7 @@ needs. For this we use a command named ``syncdb``.
In this case, syncdb will notice it's the first run, and proposes that you
create a superuser. Let's go ahead and do that (you may have to hit Ctrl-C to
-quit the test server first)::
+quit the development server first)::
python manage.py syncdb
@@ -601,12 +601,12 @@ password for the superuser.:::
Superuser created successfully.
-Let's see if that worked - try firing up the test server again::
+Let's see if that worked - try firing up the development server again::
python manage.py runserver
And if you go back to ``http://localhost/admin/``, you should see the Django
-login screen::
+login screen:
.. image:: /static/images/django_admin_login.png
@@ -1034,7 +1034,7 @@ And run the tests again::
.
----------------------------------------------------------------------
- Ran 323 tests in 2.402s
+ Ran 1 tests in 0.402s
OK
@@ -1071,8 +1071,8 @@ three lines inside the polls app called, ``polls/admin.py``:
.. sourcecode:: python
:filename: mysite/polls/admin.py
- from polls.models import Poll
from django.contrib import admin
+ from polls.models import Poll
admin.site.register(Poll)
View
@@ -24,8 +24,8 @@ Here's an outline of what we're going to do:
Inspecting the admin site to decide what to test next
-----------------------------------------------------
-Let's fire up the test server, and do a bit of browsing around the admin site -
-that way we can figure out what we want the "Polls" bit to look like.::
+Let's fire up the development server, and do a bit of browsing around the admin
+site - that way we can figure out what we want the "Polls" bit to look like.::
python manage.py runserver
@@ -188,7 +188,7 @@ let's call that the end of our FT - you can get rid of the ``self.fail``.
That's it for now - if you've lost track in amongst all the copy & pasting, you
can compare your version to mine, which is hosted here:
-https://github.com/hjwp/Test-Driven-Django-Tutorial/blob/master/fts/tests.py
+https://github.com/hjwp/Test-Driven-Django-Tutorial/blob/master/mysite/fts/tests.py`
Human-readable names for models and their attributes
@@ -451,7 +451,7 @@ And let's do a unit test run::
AttributeError: 'Choice' object has no attribute 'save'
----------------------------------------------------------------------
- Ran 326 tests in 2.745s
+ Ran 4 tests in 0.745s
FAILED (errors=1)
@@ -595,7 +595,7 @@ server using ``manage.py runserver`` and check for yourself? Remember, you may
need to ``syncdb``... Alternatively you can add a ``time.sleep(10)`` to the FT
just before the error, and that will let you see what's happening)
-Let's make 'votes' default to 0, by adding a new test in ``tests.py``
+Let's make 'votes' default to 0, by adding a new test in ``polls/tests.py``
.. sourcecode:: python
:filename: mysite/polls/tests.py
View
@@ -260,7 +260,7 @@ that will deal with producing a response for them. These functions are called
(*This is essentially an MVC pattern, there's some discussion of it here:*
https://docs.djangoproject.com/en/dev/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names)
-Let's add a new test to ``tests.py``. I'm going to use the Django Test Client,
+Let's add a new test to ``polls/tests.py``. I'm going to use the Django Test Client,
which has some helpful features for testing views. More info here:
https://docs.djangoproject.com/en/1.4/topics/testing/
@@ -285,7 +285,6 @@ We'll create a new class to test our home page view:
self.assertIn(poll1.question, response.content)
self.assertIn(poll2.question, response.content)
-Don't forget the import at the top!
Now, our first run of the tests will probably complain of a with
``TemplateDoesNotExist: 404.html``. Django wants us to create a template for
@@ -443,7 +442,7 @@ template system - that will provide a much more natural way to write web pages.
The Django TestCase lets us check whether a response was rendered using a
template, by using a special method response called ``assertTemplateUsed``, so
-let's use that. In ``tests.py``, add an extra check to our view test:
+let's use that. In ``polls/tests.py``, add an extra check to our view test:
.. sourcecode:: python
:filename: mysite/polls/tests.py
@@ -496,7 +495,6 @@ That should give us a folder structure like this::
| | `-- admin_user.json
| |-- __init__.py
| |-- models.py
- | |-- test_polls.py
| |-- tests.py
| `-- views.py
|-- manage.py
@@ -512,7 +510,7 @@ That should give us a folder structure like this::
|-- templates
| `-- home.html
|-- tests.py
- `-- views.py
+ `-- views.py
Edit ``home.html`` with your favourite editor,
@@ -763,7 +761,7 @@ Let's use the ``reverse`` function in our tests. Its first argument is the
name of the view that handles the url, and we can also specify some arguments.
We'll be making a view for seeing an individual `Poll` object, so we'll
probably find the poll using its ``id``. Here's what that translates to in
-``tests.py``:
+``polls/tests.py``:
.. sourcecode:: python
:filename: mysite/polls/tests.py
@@ -781,8 +779,8 @@ probably find the poll using its ``id``. Here's what that translates to in
response = self.client.get('/')
- template_names_used = [t.name for t in response.templates]
- self.assertIn('home.html', template_names_used)
+ # check we've used the right template
+ self.assertTemplateUsed(response, 'home.html')
# check we've passed the polls to the template
polls_in_context = response.context['polls']
@@ -798,6 +796,11 @@ probably find the poll using its ``id``. Here's what that translates to in
poll2_url = reverse('polls.views.poll', args=[poll2.id,])
self.assertIn(poll2_url, response.content)
+
+Note I've also changed the name of the test, from
+`test_root_url_shows_all_polls` to `test_root_url_shows_links_to_all_polls`, which
+is more descriptive of what we're doing now.
+
Running this (``python manage.py test polls``) gives::
======================================================================
View
@@ -307,7 +307,7 @@ https://docs.djangoproject.com/en/1.4/intro/tutorial04/
But Django can do even better than that - Django's forms system will generate
radio buttons for us, if we can just give it the right incantations. Let's
-create a new test in ``tests.py``:
+create a new test in ``polls/tests.py``:
.. sourcecode:: python
@@ -504,7 +504,7 @@ Ah yes, we still haven't actually *used* the form yet! Let's go back to our
# check we've passed in a form of the right type
self.assertTrue(isinstance(response.context['form'], PollVoteForm))
- # and check the check the form is being used in the template,
+ # and check the form is being used in the template,
# by checking for the choice text
self.assertIn(choice1.choice, response.content)
self.assertIn(choice2.choice, response.content)
View
@@ -85,11 +85,11 @@ If you run the FTs, you should see something like this::
What's happening is that clicking the submit button has no effect - we just
stay on the voting page. So, we'll need to wire up our view so that it deals
-with form submission. Let's open up ``tests.py``. We need to find the test
+with form submission. Let's open up ``polls/tests.py``. We need to find the test
that deals with our view.
At this point, you might find it's getting a little hard to find your way
-around ``tests.py`` - the file is getting a little cluttered. I think it's
+around ``polls/tests.py`` - the file is getting a little cluttered. I think it's
time to do some *refactoring*, and move things around a bit.
@@ -135,7 +135,7 @@ this:
* add a ``__init__.py`` file inside the ``tests`` folder, to make it into
an importable Python module
- * move the current ``tests.py`` into the ``tests`` folder
+ * move the current ``polls/tests.py`` into the ``polls/tests/`` folder
* finally, ``import`` all of the tests from ``tests.py`` into the
``__init__.py``
@@ -640,17 +640,17 @@ Let's try re-running our tests now::
FAILED (failures=1, errors=1)
- Oh no! Bad to worse! Our percentage function really is refusing to make our
- lives easy - it's susceptible to zero-division errors, and it's producing
- floats rather than nice printable percentages... Let's fix that. (but, again,
- notice the way it's the tests picking up all these little bugs for us, rather
- than us having to try and anticipate them all in advance, or test all the edge
- cases manually...)
+Oh no! Bad to worse! Our percentage function really is refusing to make our
+lives easy - it's susceptible to zero-division errors, and it's producing
+floats rather than nice printable percentages... Let's fix that. (but, again,
+notice the way it's the tests picking up all these little bugs for us, rather
+than us having to try and anticipate them all in advance, or test all the edge
+cases manually...)
- So, let's make our percentage function return a proper, accurate float
- representation of the percentage (or as accurate as floating-point arithmetic
- will allow), and we'll handle the presentation issues in the template. We'll
- also make it handle the 0-case
+So, let's make our percentage function return a proper, accurate float
+representation of the percentage (or as accurate as floating-point arithmetic
+will allow), and we'll handle the presentation issues in the template. We'll
+also make it handle the 0-case.
.. sourcecode:: python
:filename: mysite/polls/tests/test_models.py

0 comments on commit 230fba8

Please sign in to comment.