title: Django Test Case
body: <p>I am learning how to use Django Test Infrastructure. Very often, it is hard to come up with a good test case.</p>
  
  <p>Here is one test case in Tango with Django tutorial.</p>
  
  <pre><code>class CategoryMethodTests(TestCase):
  
      def test_ensure_views_are_positive(self):
  
          """
          ensure_views_are_positive should results True for categories where views are zero or positive
          """
          cat = Category(name='test',views=-1, likes=0)
          cat.save()
          self.assertEqual((cat.views &gt;= 0), True)
  </code></pre>
  
  <p>Here is the Category class in models.py</p>
  
  <pre><code>class Category(models.Model):
      name = models.CharField(max_length=128, unique=True)
      views = models.IntegerField(default=0)
      likes = models.IntegerField(default=0)
      slug = models.SlugField(unique=True, default=uuid.uuid1)
  
      def save(self, *args, **kwargs):
          self.slug = slugify(self.name)
          super(Category, self).save(*args, **kwargs) #python MRO
  
      def __str__(self):
          return self.name
  </code></pre>
  
  <p>I added this line into save method</p>
  
  <pre><code>   def save(self, *args, **kwargs):
         self.slug = slugify(self.name)
         if self.views &gt;=0:
             super(Category, self).save(*args, **kwargs) #python MRO
  </code></pre>
  
  <p>The test case still fails. There are two questions:
  1. How to make the test case pass;
  2. How to handle the else case (self.views &lt; 0)? </p>
  
  <p><strong>Thank Alasdair for the quick answer.</strong> </p>
  
  <p>Follow-up:
  I am testing the home page called "Index" views. Here are the test code:</p>
  
  <pre><code>class IndexViewTests(TestCase):
      def test_index_view_with_no_categories(self):
          response = self.client.get(reverse('index'))
          self.assertEqual(response.status_code, 200)
          self.assertQuerysetEqual(response.context['categories'], [])
  
  
  
     def **add_category**(name, views, likes):
          c = Category.objects.get_or_create(name=name)[0]
          c.views = views
          c.likes = likes
          c.save()
  
      def test_index_view_with_categories(self):
          add_category('test', 1, 1)
          add_category('temp', 1, 1)
          add_category('tmp', 1, 1)
          add_category('tmp test temp', 1, 1)
  
          response = self.client.get(reverse('index'))
          self.assertEqual(response.status_code, 200)
          self.assertContains(response, "tmp test temp")
  
          num_cats = len(response.context['categories'])
          self.assertEqual(num_cats, 4)
  </code></pre>
  
  <p>When I run the test case, it fails:</p>
  
  <p><strong>add_category('test', 1, 1)
  NameError: name 'add_category' is not defined</strong></p>
  
  <p>The add_category is defined in the class IndexViewTests. How shall this error be fixed? Thanks.</p>