Code

Opened 7 years ago

Closed 7 years ago

#5646 closed (wontfix)

Add Testing Section

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

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.

Attachments (0)

Change History (1)

comment:1 Changed 7 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.