I am using Flask with Azure Functions as described by Microsoft, e.g. here: https://github.com/Azure-Samples/flask-app-on-azure-functions/ So the code in the Azure function file __ init__.py follows this concept:
import azure.functions as func
from api.app import application
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
"""Each request is redirected to the WSGI handler.
"""
return func.WsgiMiddleware(application).handle(req, context)
and then the main code follows in app.py
from resources.users import Users
application = Flask(__name__)
api = Api(application)
# API routes
api.add_resource(Users, '/api/users')
Example unit test
class TestRoute(unittest.TestCase):
def test_users(self):
req = func.HttpRequest(
method='GET',
body=None,
url='/users'
resp = main(req)
self.assertEqual(resp.status_code, 200)
When I try to run the test, I am missing the context object in the __ init__.py. So somehow I need to mock this context object, but I don't know how to do this. Any hints? When printing this object it says: <azure_functions_worker.bindings.context.Context object at 0x7f4bb88504c0>