Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #20165 - Updated testing example to use django.test.Tes…

…tCase.

Thanks Lorin Hochstein.

Backport of 84d8b24 from master.
  • Loading branch information...
commit 7f4269229cd6ca0ef94a34fd045e3ced9bdd57d0 1 parent 20bed77
Tim Graham authored May 14, 2013

Showing 1 changed file with 16 additions and 12 deletions. Show diff stats Hide diff stats

  1. 28  docs/topics/testing/overview.txt
28  docs/topics/testing/overview.txt
@@ -56,20 +56,24 @@ places:
56 56
   directory that holds ``models.py``. Again, the test runner looks for any
57 57
   subclass of :class:`unittest.TestCase` in this module.
58 58
 
59  
-Here is an example :class:`unittest.TestCase` subclass::
  59
+Here is an example which subclasses from :class:`django.test.TestCase`,
  60
+which is a subclass of :class:`unittest.TestCase` that runs each test inside a
  61
+transaction to provide isolation::
60 62
 
61  
-    from django.utils import unittest
  63
+    from django.test import TestCase
62 64
     from myapp.models import Animal
63 65
 
64  
-    class AnimalTestCase(unittest.TestCase):
  66
+    class AnimalTestCase(TestCase):
65 67
         def setUp(self):
66  
-            self.lion = Animal(name="lion", sound="roar")
67  
-            self.cat = Animal(name="cat", sound="meow")
  68
+            Animal.objects.create(name="lion", sound="roar")
  69
+            Animal.objects.create(name="cat", sound="meow")
68 70
 
69 71
         def test_animals_can_speak(self):
70 72
             """Animals that can speak are correctly identified"""
71  
-            self.assertEqual(self.lion.speak(), 'The lion says "roar"')
72  
-            self.assertEqual(self.cat.speak(), 'The cat says "meow"')
  73
+            lion = Animal.objects.get(name="lion")
  74
+            cat = Animal.objects.get(name="cat")
  75
+            self.assertEqual(lion.speak(), 'The lion says "roar"')
  76
+            self.assertEqual(cat.speak(), 'The cat says "meow"')
73 77
 
74 78
 When you :ref:`run your tests <running-tests>`, the default behavior of the test
75 79
 utility is to find all the test cases (that is, subclasses of
@@ -93,11 +97,11 @@ For more details about :mod:`unittest`, see the Python documentation.
93 97
     be sure to create your test classes as subclasses of
94 98
     :class:`django.test.TestCase` rather than :class:`unittest.TestCase`.
95 99
 
96  
-    In the example above, we instantiate some models but do not save them to
97  
-    the database. Using :class:`unittest.TestCase` avoids the cost of running
98  
-    each test in a transaction and flushing the database, but for most
99  
-    applications the scope of tests you will be able to write this way will
100  
-    be fairly limited, so it's easiest to use :class:`django.test.TestCase`.
  100
+    Using :class:`unittest.TestCase` avoids the cost of running each test in a
  101
+    transaction and flushing the database, but if your tests interact with
  102
+    the database their behavior will vary based on the order that the test
  103
+    runner executes them. This can lead to unit tests that pass when run in
  104
+    isolation but fail when run in a suite.
101 105
 
102 106
 .. _running-tests:
103 107
 

0 notes on commit 7f42692

Please sign in to comment.
Something went wrong with that request. Please try again.