It can be useful when you want to test a resource method in isolation to manually create a Request and Response object and pass them directly to the resource method.
def test_my_resource():
resource = MyResource()
req, resp = Request(helpers.create_environ()), Response()
resource.on_get(req, resp)
There are some gotchas with manually creating the environ and it could be much easier.
testing.helpers which creates a Request and Response object.json, body, and streamContent-LengthContent-Type TestClient which will utilize the global method but fill in some of the environment details and utilize application specific Request and Response.def create_test_request_response(json: dict = None, body: string = None, context: dict = None, **env):
env['body'] = (
(body if body is not None else None)
or (json.dumps(json) if json else '')
)
env = falcon.testing.create_environ(**env,)
req = falcon.Request(env)
req.context.update(context or {})
return (req, falcon.Response())
def test_my_resource():
resource = MyResource()
req, resp = create_test_request_response(json={'hello': 'world'})
resource.on_get(req, resp)
@hynek, is this close to what you were talking about during our open space regarding testing?
Yep, something like that! Just let me set context, url/fields, access_route, and body/json and I should be fine I think.
As it turns out, I need this for dual-testing ASGI/WSGI so I'm working on it as part of the ASGI branch.
@kgriffs :ping_pong: Friendly ping checking if this is still on your radar.
Looks like we now have create_req() and create_asgi_req(), although they probably don't fully live up to @hynek 's requirements yet :thinking:
Yep, something like that! Just let me set context, url/fields, access_route, and body/json and I should be fine I think.
I feel like we have made good progress on this. Perhaps close it and we can create a followup issue if someone would really like additional features?
IMHO we should either document the pattern you laid out in the description, or add a helper to also create a Request, because it may be unclear you can instantiate it directly like that.
Resolved fixed as part of ASGI developments, see:
The remaining work items and documentation improvements were deferred to https://github.com/falconry/falcon/issues/1793.
Thanks, I think it makes sense to delay the rest of this to 3.1 so that we can get 3.0 out that match sooner.
Most helpful comment
As it turns out, I need this for dual-testing ASGI/WSGI so I'm working on it as part of the ASGI branch.