Loading django/test/testcases.py +5 −1 Original line number Diff line number Diff line Loading @@ -210,6 +210,10 @@ class DocTestRunner(doctest.DocTestRunner): transaction.rollback_unless_managed(using=conn) class TransactionTestCase(unittest.TestCase): # The class we'll use for the test client self.client. # Can be overridden in derived classes. client_class = Client def _pre_setup(self): """Performs any pre-test setup. This includes: Loading Loading @@ -251,7 +255,7 @@ class TransactionTestCase(unittest.TestCase): set up. This means that user-defined Test Cases aren't required to include a call to super().setUp(). """ self.client = Client() self.client = self.client_class() try: self._pre_setup() except (KeyboardInterrupt, SystemExit): Loading docs/topics/testing.txt +24 −0 Original line number Diff line number Diff line Loading @@ -1086,6 +1086,30 @@ This means, instead of instantiating a ``Client`` in each test:: response = self.client.get('/customer/index/') self.failUnlessEqual(response.status_code, 200) Customizing the test client ~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 1.3 .. attribute:: TestCase.client_class If you want to use a different Client class (for example, a subclass with customized behavior), you can use the :attr:`~TestCase.client_class` class attribute to specify a custom ``Client`` class in your test case:: from django.test import TestCase from django.test.client import Client class MyTestClient(Client): # .. specialized methods for your environment .. class MyTest(TestCase): client_class = MyTestClient def test_my_stuff(self): # .. Here self.client is an instance of MyTestClient .. .. _topics-testing-fixtures: Fixture loading Loading tests/modeltests/test_client/models.py +12 −0 Original line number Diff line number Diff line Loading @@ -457,3 +457,15 @@ class CSRFEnabledClientTests(TestCase): # The CSRF-enabled client rejects it response = csrf_client.post('/test_client/post_view/', {}) self.assertEqual(response.status_code, 403) class CustomTestClient(Client): i_am_customized = "Yes" class CustomTestClientTest(TestCase): client_class = CustomTestClient def test_custom_test_client(self): """A test case can specify a custom class for self.client.""" self.assertEqual(hasattr(self.client, "i_am_customized"), True) Loading
django/test/testcases.py +5 −1 Original line number Diff line number Diff line Loading @@ -210,6 +210,10 @@ class DocTestRunner(doctest.DocTestRunner): transaction.rollback_unless_managed(using=conn) class TransactionTestCase(unittest.TestCase): # The class we'll use for the test client self.client. # Can be overridden in derived classes. client_class = Client def _pre_setup(self): """Performs any pre-test setup. This includes: Loading Loading @@ -251,7 +255,7 @@ class TransactionTestCase(unittest.TestCase): set up. This means that user-defined Test Cases aren't required to include a call to super().setUp(). """ self.client = Client() self.client = self.client_class() try: self._pre_setup() except (KeyboardInterrupt, SystemExit): Loading
docs/topics/testing.txt +24 −0 Original line number Diff line number Diff line Loading @@ -1086,6 +1086,30 @@ This means, instead of instantiating a ``Client`` in each test:: response = self.client.get('/customer/index/') self.failUnlessEqual(response.status_code, 200) Customizing the test client ~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 1.3 .. attribute:: TestCase.client_class If you want to use a different Client class (for example, a subclass with customized behavior), you can use the :attr:`~TestCase.client_class` class attribute to specify a custom ``Client`` class in your test case:: from django.test import TestCase from django.test.client import Client class MyTestClient(Client): # .. specialized methods for your environment .. class MyTest(TestCase): client_class = MyTestClient def test_my_stuff(self): # .. Here self.client is an instance of MyTestClient .. .. _topics-testing-fixtures: Fixture loading Loading
tests/modeltests/test_client/models.py +12 −0 Original line number Diff line number Diff line Loading @@ -457,3 +457,15 @@ class CSRFEnabledClientTests(TestCase): # The CSRF-enabled client rejects it response = csrf_client.post('/test_client/post_view/', {}) self.assertEqual(response.status_code, 403) class CustomTestClient(Client): i_am_customized = "Yes" class CustomTestClientTest(TestCase): client_class = CustomTestClient def test_custom_test_client(self): """A test case can specify a custom class for self.client.""" self.assertEqual(hasattr(self.client, "i_am_customized"), True)