Opened 11 years ago
Closed 11 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 , 11 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
| Version: | 1.8 → master |
comment:2 by , 11 years ago
| Has patch: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 11 years ago
| Patch needs improvement: | set |
|---|
comment:4 by , 11 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
In 4525a0c4: