Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14378 -- Made the test client class customizable. Thanks to Ne…

…d Batchelder for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14058 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b7a73cf9296a2629a45d4b303c2b9207d3c71c40 1 parent 816c7d1
Russell Keith-Magee authored October 09, 2010
6  django/test/testcases.py
@@ -210,6 +210,10 @@ def report_unexpected_exception(self, out, test, example, exc_info):
210 210
             transaction.rollback_unless_managed(using=conn)
211 211
 
212 212
 class TransactionTestCase(unittest.TestCase):
  213
+    # The class we'll use for the test client self.client.
  214
+    # Can be overridden in derived classes.
  215
+    client_class = Client
  216
+
213 217
     def _pre_setup(self):
214 218
         """Performs any pre-test setup. This includes:
215 219
 
@@ -251,7 +255,7 @@ def __call__(self, result=None):
251 255
         set up. This means that user-defined Test Cases aren't required to
252 256
         include a call to super().setUp().
253 257
         """
254  
-        self.client = Client()
  258
+        self.client = self.client_class()
255 259
         try:
256 260
             self._pre_setup()
257 261
         except (KeyboardInterrupt, SystemExit):
24  docs/topics/testing.txt
@@ -1086,6 +1086,30 @@ This means, instead of instantiating a ``Client`` in each test::
1086 1086
             response = self.client.get('/customer/index/')
1087 1087
             self.failUnlessEqual(response.status_code, 200)
1088 1088
 
  1089
+Customizing the test client
  1090
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1091
+
  1092
+.. versionadded:: 1.3
  1093
+
  1094
+.. attribute:: TestCase.client_class
  1095
+
  1096
+If you want to use a different Client class (for example, a subclass
  1097
+with customized behavior), you can use the
  1098
+:attr:`~TestCase.client_class` class attribute to specify a custom
  1099
+``Client`` class in your test case::
  1100
+
  1101
+    from django.test import TestCase
  1102
+    from django.test.client import Client
  1103
+
  1104
+    class MyTestClient(Client):
  1105
+        # .. specialized methods for your environment ..
  1106
+
  1107
+    class MyTest(TestCase):
  1108
+        client_class = MyTestClient
  1109
+
  1110
+        def test_my_stuff(self):
  1111
+            # .. Here self.client is an instance of MyTestClient ..
  1112
+
1089 1113
 .. _topics-testing-fixtures:
1090 1114
 
1091 1115
 Fixture loading
12  tests/modeltests/test_client/models.py
@@ -457,3 +457,15 @@ def test_csrf_enabled_client(self):
457 457
         # The CSRF-enabled client rejects it
458 458
         response = csrf_client.post('/test_client/post_view/', {})
459 459
         self.assertEqual(response.status_code, 403)
  460
+
  461
+
  462
+class CustomTestClient(Client):
  463
+    i_am_customized = "Yes"
  464
+
  465
+class CustomTestClientTest(TestCase):
  466
+    client_class = CustomTestClient
  467
+
  468
+    def test_custom_test_client(self):
  469
+        """A test case can specify a custom class for self.client."""
  470
+        self.assertEqual(hasattr(self.client, "i_am_customized"), True)
  471
+

0 notes on commit b7a73cf

Please sign in to comment.
Something went wrong with that request. Please try again.