My Environment:
Python3.7
Flask==1.0.2
pytest==4.3.1
I was looking for a method to retrieve both request object and response object in pytest function. And then I met across this unit testing snippet.
http://flask.pocoo.org/snippets/58/
and below is what I did.
def test_busking(session, monkeypatch, report=Report()):
with app.test_request_context('/busking/zones'):
# call the before funcs
rv = app.preprocess_request()
if rv != None:
response = app.make_response(rv)
else:
# do the main dispatch
rv = app.dispatch_request()
response = app.make_response(rv)
# now do the after funcs
response = app.process_response(response)
assert flask.request
assert response
It works well In my environment. I was able to get both objects. What my questions are...
dispatch_request() is called when only
rv is None. why not when rv is something else?process_response() also called when
rv is Nonewhy is that?it seams like preprocess_request() calls
before_request_funcsand rv is the return value of the lastbefore_request_function... it looks like rv has nothing to do with dispatching request please but I know It has to be something... tell me what I'm wrong about.
There are still tons of things I don't understand in preprocess_request(), make_response(), dispatch_request(), process_response(). I think rv and process_request() are playing great roles but I'm not sure. I would be more than greatful if you also explain what is going on background.