~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

Viewing changes to docs/topics/testing/overview.txt

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-08-13 16:49:39 UTC
  • mfrom: (1.1.22) (4.4.28 sid)
  • Revision ID: package-import@ubuntu.com-20130813164939-ct6oweybhkuyq4tt
Tags: 1.5.2-1
* New upstream security release.
  https://www.djangoproject.com/weblog/2013/aug/13/security-releases-issued/
  - Cross-site scripting (XSS) in admin interface
  - Possible XSS via is_safe_url

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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
 
906
910
 
907
911
* A ``TestCase``, on the other hand, does not truncate tables after a test.
908
912
  Instead, it encloses the test code in a database transaction that is rolled
909
 
  back at the end of the test.  It also prevents the code under test from
910
 
  issuing any commit or rollback operations on the database, to ensure that the
911
 
  rollback at the end of the test restores the database to its initial state.
 
913
  back at the end of the test. Both explicit commits like
 
914
  ``transaction.commit()`` and implicit ones that may be caused by
 
915
  ``Model.save()`` are replaced with a ``nop`` operation. This guarantees that
 
916
  the rollback at the end of the test restores the database to its initial
 
917
  state.
912
918
 
913
919
  When running on a database that does not support rollback (e.g. MySQL with the
914
920
  MyISAM storage engine), ``TestCase`` falls back to initializing the database
915
921
  by truncating tables and reloading initial data.
916
922
 
 
923
.. warning::
 
924
 
 
925
    While ``commit`` and ``rollback`` operations still *appear* to work when
 
926
    used in ``TestCase``, no actual commit or rollback will be performed by the
 
927
    database. This can cause your tests to pass or fail unexpectedly. Always
 
928
    use ``TransactionalTestCase`` when testing transactional behavior.
 
929
 
917
930
.. note::
918
931
 
919
932
    .. versionchanged:: 1.5
1094
1107
.. note::
1095
1108
 
1096
1109
    ``LiveServerTestCase`` makes use of the :doc:`staticfiles contrib app
1097
 
    </howto/static-files>` so you'll need to have your project configured
 
1110
    </howto/static-files/index>` so you'll need to have your project configured
1098
1111
    accordingly (in particular by setting :setting:`STATIC_URL`).
1099
1112
 
1100
1113
.. note::