| 63 | | You can choose the test framework you like, depending on which syntax you |
| 64 | | prefer, or you can mix and match, using one framework for some of your code and |
| 65 | | the other framework for other code. You can also use any *other* Python test |
| 66 | | frameworks, as we'll explain in a bit. |
| | 73 | .. admonition:: unittest2 |
| | 74 | |
| | 75 | .. versionchanged:: 1.3 |
| | 76 | |
| | 77 | Python 2.7 introduced some major changes to the unittest library, |
| | 78 | adding some extremely useful features. To ensure that every Django |
| | 79 | project can benefit from these new features, Django ships with a |
| | 80 | copy of unittest2_, a copy of the Python 2.7 unittest library, |
| | 81 | backported for Python 2.4 compatibility. |
| | 82 | |
| | 83 | To access this library, Django provides the |
| | 84 | ``django.utils.unittest`` module alias. If you are using Python |
| | 85 | 2.7, or you have installed unittest2 locally, Django will map the |
| | 86 | alias to the installed version of the unittest library. Otherwise, |
| | 87 | Django will use it's own bundled version of unittest2. |
| | 88 | |
| | 89 | To use this alias, simply use:: |
| | 90 | |
| | 91 | from django.utils import unittest |
| | 92 | |
| | 93 | wherever you would have historically used:: |
| | 94 | |
| | 95 | import unittest |
| | 96 | |
| | 97 | If you want to continue to use the base unittest libary, you can -- |
| | 98 | you just won't get any of the nice new unittest2 features. |
| | 99 | |
| | 100 | .. _unittest2: http://pypi.python.org/pypi/unittest2 |
| | 101 | |
| | 102 | For a given Django application, the test runner looks for unit tests in two |
| | 103 | places: |
| | 104 | |
| | 105 | * The ``models.py`` file. The test runner looks for any subclass of |
| | 106 | ``unittest.TestCase`` in this module. |
| | 107 | |
| | 108 | * A file called ``tests.py`` in the application directory -- i.e., the |
| | 109 | directory that holds ``models.py``. Again, the test runner looks for any |
| | 110 | subclass of ``unittest.TestCase`` in this module. |
| | 111 | |
| | 112 | Here is an example ``unittest.TestCase`` subclass:: |
| | 113 | |
| | 114 | from django.utils import unittest |
| | 115 | from myapp.models import Animal |
| | 116 | |
| | 117 | class AnimalTestCase(unittest.TestCase): |
| | 118 | def setUp(self): |
| | 119 | self.lion = Animal.objects.create(name="lion", sound="roar") |
| | 120 | self.cat = Animal.objects.create(name="cat", sound="meow") |
| | 121 | |
| | 122 | def testSpeaking(self): |
| | 123 | self.assertEqual(self.lion.speak(), 'The lion says "roar"') |
| | 124 | self.assertEqual(self.cat.speak(), 'The cat says "meow"') |
| | 125 | |
| | 126 | When you :ref:`run your tests <running-tests>`, the default behavior of the |
| | 127 | test utility is to find all the test cases (that is, subclasses of |
| | 128 | ``unittest.TestCase``) in ``models.py`` and ``tests.py``, automatically build a |
| | 129 | test suite out of those test cases, and run that suite. |
| | 130 | |
| | 131 | There is a second way to define the test suite for a module: if you define a |
| | 132 | function called ``suite()`` in either ``models.py`` or ``tests.py``, the |
| | 133 | Django test runner will use that function to construct the test suite for that |
| | 134 | module. This follows the `suggested organization`_ for unit tests. See the |
| | 135 | Python documentation for more details on how to construct a complex test |
| | 136 | suite. |
| | 137 | |
| | 138 | For more details about ``unittest``, see the `standard library unittest |
| | 139 | documentation`_. |
| | 140 | |
| | 141 | .. _unittest: http://docs.python.org/library/unittest.html |
| | 142 | .. _standard library unittest documentation: unittest_ |
| | 143 | .. _suggested organization: http://docs.python.org/library/unittest.html#organizing-tests |
| 151 | | Writing unit tests |
| 152 | | ------------------ |
| 153 | | |
| 154 | | Like doctests, Django's unit tests use a Python standard library |
| 155 | | module: unittest_. This module uses a different way of defining tests, |
| 156 | | taking a class-based approach. |
| 157 | | |
| 158 | | .. admonition:: unittest2 |
| 159 | | |
| 160 | | .. versionchanged:: 1.3 |
| 161 | | |
| 162 | | Python 2.7 introduced some major changes to the unittest library, |
| 163 | | adding some extremely useful features. To ensure that every Django |
| 164 | | project can benefit from these new features, Django ships with a |
| 165 | | copy of unittest2_, a copy of the Python 2.7 unittest library, |
| 166 | | backported for Python 2.4 compatibility. |
| 167 | | |
| 168 | | To access this library, Django provides the |
| 169 | | ``django.utils.unittest`` module alias. If you are using Python |
| 170 | | 2.7, or you have installed unittest2 locally, Django will map the |
| 171 | | alias to the installed version of the unittest library. Otherwise, |
| 172 | | Django will use it's own bundled version of unittest2. |
| 173 | | |
| 174 | | To use this alias, simply use:: |
| 175 | | |
| 176 | | from django.utils import unittest |
| 177 | | |
| 178 | | wherever you would have historically used:: |
| 179 | | |
| 180 | | import unittest |
| 181 | | |
| 182 | | If you want to continue to use the base unittest libary, you can -- |
| 183 | | you just won't get any of the nice new unittest2 features. |
| 184 | | |
| 185 | | .. _unittest2: http://pypi.python.org/pypi/unittest2 |
| 186 | | |
| 187 | | As with doctests, for a given Django application, the test runner looks for |
| 188 | | unit tests in two places: |
| 189 | | |
| 190 | | * The ``models.py`` file. The test runner looks for any subclass of |
| 191 | | ``unittest.TestCase`` in this module. |
| 192 | | |
| 193 | | * A file called ``tests.py`` in the application directory -- i.e., the |
| 194 | | directory that holds ``models.py``. Again, the test runner looks for any |
| 195 | | subclass of ``unittest.TestCase`` in this module. |
| 196 | | |
| 197 | | This example ``unittest.TestCase`` subclass is equivalent to the example given |
| 198 | | in the doctest section above:: |
| 199 | | |
| 200 | | from django.utils import unittest |
| 201 | | from myapp.models import Animal |
| 202 | | |
| 203 | | class AnimalTestCase(unittest.TestCase): |
| 204 | | def setUp(self): |
| 205 | | self.lion = Animal.objects.create(name="lion", sound="roar") |
| 206 | | self.cat = Animal.objects.create(name="cat", sound="meow") |
| 207 | | |
| 208 | | def testSpeaking(self): |
| 209 | | self.assertEqual(self.lion.speak(), 'The lion says "roar"') |
| 210 | | self.assertEqual(self.cat.speak(), 'The cat says "meow"') |
| 211 | | |
| 212 | | When you :ref:`run your tests <running-tests>`, the default behavior of the |
| 213 | | test utility is to find all the test cases (that is, subclasses of |
| 214 | | ``unittest.TestCase``) in ``models.py`` and ``tests.py``, automatically build a |
| 215 | | test suite out of those test cases, and run that suite. |
| 216 | | |
| 217 | | There is a second way to define the test suite for a module: if you define a |
| 218 | | function called ``suite()`` in either ``models.py`` or ``tests.py``, the |
| 219 | | Django test runner will use that function to construct the test suite for that |
| 220 | | module. This follows the `suggested organization`_ for unit tests. See the |
| 221 | | Python documentation for more details on how to construct a complex test |
| 222 | | suite. |
| 223 | | |
| 224 | | For more details about ``unittest``, see the `standard library unittest |
| 225 | | documentation`_. |
| 226 | | |
| 227 | | .. _unittest: http://docs.python.org/library/unittest.html |
| 228 | | .. _standard library unittest documentation: unittest_ |
| 229 | | .. _suggested organization: http://docs.python.org/library/unittest.html#organizing-tests |
| 230 | | |