﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
5646	Add Testing Section	john.staff2222@…	nobody	"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.

"		closed	Documentation	dev		wontfix			Unreviewed	0	0	0	0	0	0
