23
23
.. admonition:: unittest2
25
27
Python 2.7 introduced some major changes to the ``unittest`` library,
26
adding some extremely useful features. To ensure that every Django
27
project can benefit from these new features, Django ships with a
28
copy of unittest2_, a copy of Python 2.7's ``unittest``, backported for
29
Python 2.6 compatibility.
31
To access this library, Django provides the
32
``django.utils.unittest`` module alias. If you are using Python
33
2.7, or you have installed ``unittest2`` locally, Django will map the alias
34
to it. Otherwise, Django will use its own bundled version of ``unittest2``.
36
To use this alias, simply use::
38
from django.utils import unittest
40
wherever you would have historically used::
44
If you want to continue to use the legacy ``unittest`` library, you can --
45
you just won't get any of the nice new ``unittest2`` features.
28
adding some extremely useful features. To ensure that every Django project
29
could benefit from these new features, Django used to ship with a copy of
30
Python 2.7's ``unittest`` backported for Python 2.6 compatibility.
32
Since Django no longer supports Python versions older than 2.7,
33
``django.utils.unittest`` is deprecated. Simply use ``unittest``.
47
35
.. _unittest2: https://pypi.python.org/pypi/unittest2
182
170
the tests will by default use an in-memory database (i.e., the
183
171
database will be created in memory, bypassing the filesystem
184
172
entirely!). If you want to use a different database name, specify
185
:setting:`TEST_NAME` in the dictionary for any given database in
186
:setting:`DATABASES`.
173
:setting:`NAME <TEST_NAME>` in the :setting:`TEST <DATABASE-TEST>`
174
dictionary for any given database in :setting:`DATABASES`.
176
.. versionchanged:: 1.7
178
On PostgreSQL, :setting:`USER` will also need read access to the built-in
179
``postgres`` database.
188
181
Aside from using a separate database, the test runner will otherwise
189
182
use all of the same database settings you have in your settings file:
193
186
create a new database on the system.
195
188
For fine-grained control over the character encoding of your test
196
database, use the :setting:`TEST_CHARSET` option. If you're using
197
MySQL, you can also use the :setting:`TEST_COLLATION` option to
189
database, use the :setting:`CHARSET <TEST_CHARSET>` TEST option. If you're using
190
MySQL, you can also use the :setting:`COLLATION <TEST_COLLATION>` option to
198
191
control the particular collation used by the test database. See the
199
192
:doc:`settings documentation </ref/settings>` for details of these
193
and other advanced settings.
195
.. versionchanged:: 1.7
197
The different options in the :setting:`TEST <DATABASE-TEST>` database
198
setting used to be separate options in the database settings dictionary,
199
prefixed with ``TEST_``.
202
201
.. admonition:: Finding data from your production database when running tests?
208
207
your tests. *It is a bad idea to have such import-time database queries in
209
208
your code* anyway - rewrite your code so that it doesn't do this.
210
.. versionadded:: 1.7
212
This also applies to customized implementations of
213
:meth:`~django.apps.AppConfig.ready()`.
213
217
The :ref:`advanced multi-db testing topics <topics-testing-advanced-multidb>`.
223
227
* All :class:`~django.test.TestCase` subclasses are run first.
225
* Then, all other unittests (including :class:`unittest.TestCase`,
226
:class:`~django.test.SimpleTestCase` and
229
* Then, all other Django-based tests (test cases based on
230
:class:`~django.test.SimpleTestCase`, including
227
231
:class:`~django.test.TransactionTestCase`) are run with no particular
228
232
ordering guaranteed nor enforced among them.
230
* Then any other tests (e.g. doctests) that may alter the database without
231
restoring it to its original state are run.
233
.. versionchanged:: 1.5
235
Before Django 1.5, the only guarantee was that
236
:class:`~django.test.TestCase` tests were always ran first, before any other
234
* Then any other :class:`unittest.TestCase` tests (including doctests) that may
235
alter the database without restoring it to its original state are run.
243
241
database by a given :class:`~django.test.TransactionTestCase` test, they
244
242
must be updated to be able to run independently.
244
.. _test-case-serialized-rollback:
249
Any initial data loaded in migrations will only be available in ``TestCase``
250
tests and not in ``TransactionTestCase`` tests, and additionally only on
251
backends where transactions are supported (the most important exception being
254
Django can reload that data for you on a per-testcase basis by
255
setting the ``serialized_rollback`` option to ``True`` in the body of the
256
``TestCase`` or ``TransactionTestCase``, but note that this will slow down
257
that test suite by approximately 3x.
259
Third-party apps or those developing against MyISAM will need to set this;
260
in general, however, you should be developing your own projects against a
261
transactional database and be using ``TestCase`` for most tests, and thus
262
not need this setting.
264
The initial serialization is usually very quick, but if you wish to exclude
265
some apps from this process (and speed up test runs slightly), you may add
266
those apps to :setting:`TEST_NON_SERIALIZED_APPS`.
268
Apps without migrations are not affected; ``initial_data`` fixtures are
246
271
Other test conditions
247
272
---------------------