Opened 10 years ago

Closed 10 years ago

#24773 closed New feature (fixed)

Add a JSON response to the test client

Reported by: Andy McKay Owned by: Andy McKay
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I've got a few Django projects that are just JSON APIs and return JSON. If you want to test those in the test client, you have to parse the response into JSON. Each and every test call. For example:

def test_ok(self):
   assert json.loads(self.client.get(url).content)['modified']

What would be nice is to have json on the test client, which can be parsed if the response is application/json. We can mimic the requests interface here:

http://docs.python-requests.org/en/latest/api/?highlight=json#requests.Response.json

So it would look like this:

def test_ok(self):
   assert self.client.get(url).json()['modified']

I wouldn't suggest we do this for every content type, but JSON is everywhere. Like glitter.

Change History (4)

comment:1 by Andy McKay, 10 years ago

Owner: changed from nobody to Andy McKay
Status: newassigned
Version: 1.8master

comment:2 by Tim Graham, 10 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 10 years ago

Patch needs improvement: set

comment:4 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 4525a0c4:

Fixed #24773 -- Added a json() method on test client responses.

Note: See TracTickets for help on using tickets.
Back to Top