Test Client as Testing Tool for get() and post() Requests in Django

by Shahid Ayoub

Test client is a Python class that acts as a dummy Web browser, allowing you to test your views and interact with your Django-powered application programmatically.The test client does not require Web server to be running. In fact, it will run just fine with no Web server running at all! That’s because it avoids overhead of HTTP and deals directly with Django framework. This helps make unit tests run quickly.

 

In order to test get and post requests in our Django application, we first need to import Client from django.test module of django. After that we will create an instance of Client, which will be used to send get() and post() requests.

At first we will elaborate a get request on a particular url path and check response as shown in the code snippet below: 

 from django.test import TestCase

 from django.test import Client

 class ExamplelGetTest(TestCase):

       @classmethod
       def setUpClass(self):
           # creating instance of a client.
           self.client = Client()

       def test_getLogin(self):
           # Issue a GET request.
           response = self.client.get('/application/login/')

           # Check that the response is 200 OK.
           self.assertEqual(response.status_code, 200)

 

Here, we are initializing a Client object as self.client in setUpClass method which is common to all methods of the testcase ExampleGetTest. In test_getLogin method we are sending a get request to login page which returns the response object. At last, HTTP status of the response, status_code is checked using assertion method assertEqual. If it is equal to 200, then we have successfully rendered login page in our dummy browser. The result of the above test can be checked using the below mentioned command: 

python manage.py test  ExamplelGetTest

This will give the following result: 

Creating test database for alias 'default'...

 
.
----------------------------------------------------------------------

Ran 1 test in 0.522s

OK

Destroying test database for alias 'default'...

 

Now let us look at how we can send post request to the application. POST requests are a common (and good) way for the user to enter data into the application. Moreover we don’t require to worry about CSRF, Django disables CSRF checks when running the test suite by default. The code snippet for the post request can be shown below: 

from django.test import TestCase

from django.test import Client

 

class ExapmplePostTest(TestCase):

@classmethod

def setUpClass(self):

      self.client = Client()

 

''' Adding Account into the application '''

def test_addAccount(self):

      response = self.client.post('/',{'username':'name','password':'pass',

’email:’mail@mail.com’’})

      self.assertEqual(response.status_code, 302)

 

Here, in test_addAccount method we are sending a post request to add an account in the application which returns the response object. HTTP status of the response, status_code is checked using assertion method assertEqual. If it is equal to 302, then we have successfully posted the data in the application. The result of the above test can be checked using the below mentioned command: 

python manage.py test  ExamplelPostTest

This will give the following result:

 

Creating test database for alias 'default'...

.

----------------------------------------------------------------------

Ran 1 test in 0.522s

OK

Destroying test database for alias 'default'...

 

Incase if there is any wrong field in the post request we can get an assertion error. The result will be like this:

 

Creating test database for alias 'default'...

F

======================================================================

FAIL: test_addAccount(Account.tests.ExapmplePostTest)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/path to project/appname/tests.py", line 128, in test_addAccount

    self.assertEqual(response.status_code, 302)

AssertionError: 200 != 302

 

----------------------------------------------------------------------

Ran 1 test in 0.506s

 

FAILED (failures=1)

Destroying test database for alias 'default'...


There is still a lot to discuss about Test Client but that is beyond the scope of this blog. I hope this was useful and lays the foundation for achieving more from Test client as a testing tool.

To learn more about

Contact Us

One thought on “Test Client as Testing Tool for get() and post() Requests in Django”

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts