Ticket #14378: custom-test-client.diff
File custom-test-client.diff, 2.4 KB (added by , 14 years ago) |
---|
-
django/test/testcases.py
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 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): -
tests/modeltests/test_client/models.py
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 -
docs/topics/testing.txt
1075 1075 response = self.client.get('/customer/index/') 1076 1076 self.failUnlessEqual(response.status_code, 200) 1077 1077 1078 In your sub-classes of ``TestCase`` you can specify a custom sub-class of 1079 ``Client`` to instantiate:: 1080 1081 from django.test import TestCase 1082 from django.test.client import Client 1083 1084 class MyTestClient(Client): 1085 # .. specialized methods for my environment .. 1086 1087 class MyTest(TestCase): 1088 client_class = MyTestClient 1089 1090 def test_my_stuff(self): 1091 # .. Here self.client is an instance of MyTestClient .. 1092 1078 1093 .. _topics-testing-fixtures: 1079 1094 1080 1095 Fixture loading