summaryrefslogtreecommitdiff
path: root/docs/topics/testing.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/topics/testing.txt')
-rw-r--r--docs/topics/testing.txt118
1 files changed, 115 insertions, 3 deletions
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
index cec6002b7b..d066ca086a 100644
--- a/docs/topics/testing.txt
+++ b/docs/topics/testing.txt
@@ -400,7 +400,7 @@ Some of the things you can do with the test client are:
a template context that contains certain values.
Note that the test client is not intended to be a replacement for Twill_,
-Selenium_, or other "in-browser" frameworks. Django's test client has
+Windmill_, or other "in-browser" frameworks. Django's test client has
a different focus. In short:
* Use Django's test client to establish that the correct view is being
@@ -409,10 +409,15 @@ a different focus. In short:
* Use in-browser frameworks such as Twill and Selenium to test *rendered*
HTML and the *behavior* of Web pages, namely JavaScript functionality.
-A comprehensive test suite should use a combination of both test types.
+A comprehensive test suite should use a combination of both test types. Which
+is why Django makes it easy to integrate with 3rd party test runners via the
+:setting:`TEST_RUNNER` setting. For convenience, Django ships a runner for
+the framework used in testing the :ref:`admin interface, <ref-contrib-admin>`
+Windmill_. Details on integrating Windmill tests with Django are available
+:ref:`here. <howto-windmill-tests>`
.. _Twill: http://twill.idyll.org/
-.. _Selenium: http://www.openqa.org/selenium/
+.. _Windmill: http://www.getwindmill.com/
Overview and a quick example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -706,6 +711,28 @@ arguments at time of construction:
and session data cleared to defaults. Subsequent requests will appear
to come from an AnonymousUser.
+
+Making mock requests
+~~~~~~~~~~~~~~~~~~~~
+.. versionadded:: 1.1
+
+Use the ``django.test.mocks.RequestFactory`` class to create mock requests. Usage is as follows.
+::
+ rf = RequestFactory()
+ get_request = rf.get('/hello/')
+ post_request = rf.post('/submit/', {'foo': 'bar'})
+
+Once you have a request object you can pass it to any view function, just as if
+ that view had been hooked up using a URLconf.
+
+
+.. class:: RequestFactory()
+ There is only one method on a ``RequestFactory``.
+
+ .. method:: RequestFactory.request(path, **request)
+ This will return a request object with the proper environment.
+
+
Testing responses
~~~~~~~~~~~~~~~~~
@@ -938,6 +965,8 @@ This means, instead of instantiating a ``Client`` in each test::
response = self.client.get('/customer/index/')
self.failUnlessEqual(response.status_code, 200)
+
+
.. _topics-testing-fixtures:
Fixture loading
@@ -999,6 +1028,8 @@ This flush/load procedure is repeated for each test in the test case, so you
can be certain that the outcome of a test will not be affected by another test,
or by the order of test execution.
+.. _topics-testing-urlconf:
+
URLconf configuration
~~~~~~~~~~~~~~~~~~~~~
@@ -1032,6 +1063,87 @@ For example::
This test case will use the contents of ``myapp.test_urls`` as the
URLconf for the duration of the test case.
+.. _topics-testing-testmodels:
+
+Test-Only Models configuration
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. versionadded:: 1.1
+
+.. attribute:: TransactionTestCase.test_models
+
+If you want to test your application with models that are only available during specific
+test cases, ``django.test.TransactionTestCase`` provides the ability to customize the models
+configuration for the duration of the execution of a test suite. If your
+``TransactionTestCase`` instance defines an ``test_models`` attribute, the ``TransactionTestCase`` will load
+the value of that attribute as a module, and load the models contained within for the
+duration of that test.
+
+For example::
+
+ from django.test import TransactionTestCase
+
+ class TestMyViews(TransactionTestCase):
+ test_models = ['test_models']
+
+ def testIndexPageView(self):
+ # Here you'd test your view using ``Client``.
+
+This test case will load the contents of ``myapp.test_models`` and add
+any subclass of ``django.db.models.Model`` to ``myapp.models``.
+
+Skipping tests bound to fail
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. versionadded: 1.1
+
+Occasionally it's helpful to specify tests that are skipped under certain
+circumstances. To accomplish this, the Django test framework offers decorators
+that you can apply to your test methods for them to be conditionally skipped.
+
+You can supply your own condition function as follows::
+
+ from django.tests.decorators import *
+
+ class TestUnderCondition(TestCase):
+
+ def _my_condition():
+ # Condition returning True if test should be run and False if it
+ # should be skipped.
+
+ @conditional_skip(_my_condition, reason='This test should be skipped sometimes')
+ def testOnlyOnTuesday(self):
+ # Test to run if _my_condition evaluates to True
+
+In addition, the Django test framework supplies a handful of skip conditions that
+handle commonly used conditions for skipping tests.
+
+``views_required(required_views=[])``
+ Does a ``urlresolver.Reverse`` on the required views supplied. Runs test only if
+ all views in ``required_views`` are in use.
+
+``modules_required(required_modules=[])``
+ Runs tests only if all modules in ``required_modules`` can be imported.
+
+``skip_specific_database(database_engine)``
+ Skips test if ``settings.DATABASE_ENGINE`` is equal to database_engine.
+
+If a test is skipped, it is added to a skipped category in the test runner and
+the test results are reported as such::
+
+ ======================================================================
+ SKIPPED: test_email_found (django.contrib.auth.tests.basic.PasswordResetTest)
+ ----------------------------------------------------------------------
+ Traceback (most recent call last):
+ File "/Users/dnaquin/Dropbox/Sandbox/django/django/test/decorators.py", line 43, in _skip
+ raise SkippedTest(reason=reason)
+ SkippedTest: Required view for this test not found: django.contrib.auth.views.password_reset
+
+ ----------------------------------------------------------------------
+ Ran 408 tests in 339.663s
+
+ FAILED (failures=1, skipped=2)
+
.. _emptying-test-outbox:
Emptying the test outbox