def ctx(app, request):
    """Push a new application context and closes it automatically
    when the test goes out of scope.

    This helps to avoid creating application contexts manually
    either by calling app.app_context() with python `with` statement or
    by pushing or popping manually.
    """
    ctx = app.app_context()
    ctx.push()

    def teardown():
        ctx.pop()

    request.addfinalizer(teardown)

    return ctx