Skip to content

Commit

Permalink
[1.5.x] Fixed #17312 - Warned about database side effects in tests.
Browse files Browse the repository at this point in the history
Thanks jcspray for the suggestion.

Backport of 7df0326 from master.
  • Loading branch information
timgraham committed Dec 18, 2012
1 parent 6d712e0 commit 2545dc5
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions docs/topics/testing.txt
Expand Up @@ -115,8 +115,8 @@ Here is an example :class:`unittest.TestCase` subclass::

class AnimalTestCase(unittest.TestCase):
def setUp(self):
self.lion = Animal.objects.create(name="lion", sound="roar")
self.cat = Animal.objects.create(name="cat", sound="meow")
self.lion = Animal(name="lion", sound="roar")
self.cat = Animal(name="cat", sound="meow")

def test_animals_can_speak(self):
"""Animals that can speak are correctly identified"""
Expand All @@ -139,6 +139,18 @@ For more details about :mod:`unittest`, see the Python documentation.

.. _suggested organization: http://docs.python.org/library/unittest.html#organizing-tests

.. warning::

If your tests rely on database access such as creating or querying models,
be sure to create your test classes as subclasses of
:class:`django.test.TestCase` rather than :class:`unittest.TestCase`.

In the example above, we instantiate some models but do not save them to
the database. Using :class:`unittest.TestCase` avoids the cost of running
each test in a transaction and flushing the database, but for most
applications the scope of tests you will be able to write this way will
be fairly limited, so it's easiest to use :class:`django.test.TestCase`.

Writing doctests
----------------

Expand Down Expand Up @@ -343,7 +355,7 @@ This convenience method sets up the test database, and puts other
Django features into modes that allow for repeatable testing.

The call to :meth:`~django.test.utils.setup_test_environment` is made
automatically as part of the setup of `./manage.py test`. You only
automatically as part of the setup of ``./manage.py test``. You only
need to manually invoke this method if you're not using running your
tests via Django's test runner.

Expand Down Expand Up @@ -1382,6 +1394,7 @@ attribute::

def test_my_stuff(self):
# Here self.client is an instance of MyTestClient...
call_some_test_code()

.. _topics-testing-fixtures:

Expand Down

0 comments on commit 2545dc5

Please sign in to comment.