unit test that code in after_this_request + call_on_close was executed after request

  Kiến thức lập trình

The example code below is similar to this answer, it runs a function after a Flask response has been returned. This works just fine.

My question is how can I unit test via the Flask.test_client that my_task() was called during the test ?

It seems that the Flask.test_client by default does not trigger after_this_request actions.

@app.route('/')
def index():
    # create response
    msg = 'Hello World!'
    @flask.after_this_request
    def add_close_action(response):
        @response.call_on_close
        def process_after_request():
            # post-response tasks
            self.my_task()
        return response
    return msg

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT