Opened 17 years ago

Closed 17 years ago

#5646 closed (wontfix)

Add Testing Section

Reported by: john.staff2222@… Owned by: nobody
Component: Documentation Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

As part of the "Your First App" - right after creating a model, a short section should be included on how to set up and use a unit test. Here's an example writeup of what we did (though we used the example of James Bennett's to do list tutorial).

Testing Your Models

Now that you have created a few models, it is important to create unit tests to insure they work. This becomes more important later when you make changes to your application and want to insure the changes work before you upload them to the web. Django has built in unit testing that makes this easy.
In your application directory, you need to create a "tests.py" file. In this file you can include the following code:

import unittest
from todo.models import List
from todo.models import Item

class ListTestCase(unittest.TestCase):

def setUp(self):

self.testlist = List.objects.create(name='TestList')

def test_Name(self):

self.assertEquals(self.testlist.name, 'TestList')

class ItemTestCase(unittest.TestCase):

def setUp(self):

self.testlist = List.objects.create(name='TestList')
self.testitem = Count.objects.create(name="TestItem",

todo_list = self.testlist)

def test_Name(self):

self.assertEquals(self.testitem.name, 'TestItem')

The "def setUp(self)" function can be used to create an instance of the model that can be used in the test functions that follow. So for the List model, we create an instance named "testlist" and have set the object name field with "TestList".

Actual test functions begin with "test" (an underscore is not necessary but makes for easy code reading). In the test function, different "assert" functions can be used - in this case an "assertEquals" function is used to make a comparison of the name field in the created object (testlist) with the actual phrase "TestList".
Now you run the tests at the command line by typing:
python manage.py test

If the object was created correctly, the names should match and you should get a test passed response like :

Ran 2 tests in 0.003s
OK

otherwise Python and Django gives you pretty specific error messages.
Note that for the Item class test, you need to create an instance of a List object, then create an Item object and fill in the "todo_list" foreign key field with the name of the newly created list object. Note that an object created in the first class does not extend to the 2nd class. You don't need to create separate classes for each model but it results in cleaner testing and coding.

Change History (1)

comment:1 by James Bennett, 17 years ago

Resolution: wontfix
Status: newclosed

This suggestion seems to duplicate Django's own model-API unit tests, which already verify that creating an object form a model class does create the expected object. Testing the views would be more interesting, but probably would need its own separate section of the tutorial, at which point one wonders why we wouldn't just link to the full testing docs.

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