#34640 closed New feature (duplicate)

Add query_params argument to test Client methods

Reported by: Dan Strokirk Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using Client.get it’s currently very easy to add new query parameters to the fake request, you can simply provide them to data argument. Very nice and ergonomic.

However, for the other HTTP methods, like post, this doesn’t work, since of course they use the same parameter for their form data. So adding a new argument to all methods would make it easier to test other HTTP methods. Since request.GET is getting renamed to query_params, that would be an excellent name for the new argument.

This would make it possible to change this type of testing code:

url = reverse("view_name", args=[obj.id]) + f"?param={val}"
self.client.post(url, data=data)

to this

url = reverse("view_name", args=[obj.id])
self.client.post(url, query_params={"param": val"}, data=data)

Client.get would still support setting params via the data argument, to avoid unnecessary deprecation churn in old projects. Using both data and query_params together in Client.get would throw a ValueError.

Taken from: https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/1

Change History (1)

comment:1 by Mariusz Felisiak, 19 months ago

Resolution: duplicate
Status: newclosed

Duplicate of #14611.

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