SimpleTestCase.client
Every test case in a django.test.*TestCase
instance has access to an instance of a Django test client. This client can be accessed as self.client
. This client is recreated for each test, so you don’t have to worry about state (such as cookies) carrying over from one test to another.
This means, instead of instantiating a Client
in each test:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import unittest from django.test import Client class SimpleTest(unittest.TestCase): def test_details( self ): client = Client() response = client.get( '/customer/details/' ) self .assertEqual(response.status_code, 200 ) def test_index( self ): client = Client() response = client.get( '/customer/index/' ) self .assertEqual(response.status_code, 200 ) |
...you can just refer to self.client
, like so:
1 2 3 4 5 6 7 8 9 10 | from django.test import TestCase class SimpleTest(TestCase): def test_details( self ): response = self .client.get( '/customer/details/' ) self .assertEqual(response.status_code, 200 ) def test_index( self ): response = self .client.get( '/customer/index/' ) self .assertEqual(response.status_code, 200 ) |
Please login to continue.