Ticket #14378: custom-test-client.diff

File custom-test-client.diff, 2.4 KB (added by Ned Batchelder, 14 years ago)

the patch

  • django/test/testcases.py

     
    210210            transaction.rollback_unless_managed(using=conn)
    211211
    212212class 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
    213217    def _pre_setup(self):
    214218        """Performs any pre-test setup. This includes:
    215219
     
    251255        set up. This means that user-defined Test Cases aren't required to
    252256        include a call to super().setUp().
    253257        """
    254         self.client = Client()
     258        self.client = self.client_class()
    255259        try:
    256260            self._pre_setup()
    257261        except (KeyboardInterrupt, SystemExit):
  • tests/modeltests/test_client/models.py

     
    457457        # The CSRF-enabled client rejects it
    458458        response = csrf_client.post('/test_client/post_view/', {})
    459459        self.assertEqual(response.status_code, 403)
     460
     461
     462class CustomTestClient(Client):
     463    i_am_customized = "Yes"
     464
     465class 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

     
    10751075            response = self.client.get('/customer/index/')
    10761076            self.failUnlessEqual(response.status_code, 200)
    10771077
     1078In 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
    10781093.. _topics-testing-fixtures:
    10791094
    10801095Fixture loading
Back to Top