[Django]-Writing a Django unit test for a function with a HttpResponse

4👍

self.client.get makes the request and returns the response so it is totally unnecessary to call myfunction directly to get the response down below.

Another thing is, HttpResponse has a property named content which can be a bytestring or an iterator that stores the response content.

In your case, you can convert that to a dictionary using json.loads and access any values just like you already are doing:

import json

def my_test(self):
    ...
    response = self.client.get(...)
    result = json.loads(response.content)
    telecasts_check = result['records'][0]['telecasts']
    ...

Leave a comment