简体   繁体   English

如何测试使用上下文的 Azure Function?

[英]How to test an Azure Function that uses Context?

I'm writing an Azure Function that uses context (for monitoring purposes), this is what my function looks like:我正在写一个使用上下文的 Azure Function(用于监视目的),这就是我的 function 的样子:

import logging
import json
import azure.functions as func

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    logging.info(
        "{}\t{}".format(
            context.invocation_id, context.function_name
        )
    )
    reponse = "foo"    
    return func.HttpResponse(json.dumps(reponse), mimetype="application/json")

And I want to write integration test for this function, like this:我想为这个 function 编写集成测试,如下所示:

import unittest
import azure.functions as func
from MyFunc import main    

class TestMyFunc(unittest.TestCase):
    def test_myfunc(self):
        req = func.HttpRequest(
            method="GET",
            body=None,
            url="/api/myfunc",
        )
        ctx = ??
        reponse = main(req, ctx) # This part fails because of the context

        self.assertEqual(
            reponse.get_body(),
            b'foo',
        )

How can I create a Context object to pass to my Azure Function?如何创建上下文object 以传递给我的 Azure Function?

Looks like Context is an ABC(Abstract Base Class) class , so you cannot directly instantiate.看起来Context是一个 ABC(Abstract Base Class) class ,所以你不能直接实例化。 Instead you could subclass the func.Context class and use the instance of that class as ctx相反,您可以子类化func.Context class并将该 class 的实例用作ctx

class MockContext(func.Context):
    def __init__(self, ii, fn, fd):
        self._invocation_id = ii
        self._function_name = fn
        self._function_directory = fd

    @property
    def invocation_id(self):
        return self._invocation_id

    @property
    def function_name(self):
        return self._function_name

    @property
    def function_directory(self):
        return self._function_directory

ctx = MockContext("Some dummy values")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何测试使用 filepath.walk 的 function - How do i test a function which uses filepath.walk 如何在使用谷歌云存储时在本地测试谷歌云 Function - How to test aGoogle Cloud Function locally when it uses google cloud storage 本地测试时如何轻松启用azure function https - How to enable azure function https easily when do local test 单元测试 Azure 事件中心触发器(Azure 函数) - Unit test Azure Event Hub Trigger (Azure Function) 如何测试在 wso2 集成器中使用连接器的 API - How to test an API that uses a connector in a wso2 integrator 已升级 Azure Function 无法从门户测试 - Upgraded Azure Function cannot test from the portal 是否有可能构建一个 azure function,它使用图形 api 调用并且可以在大量负载下处理可靠的身份验证? - is it possible to build a azure function which uses graph api call and can handle reliable authentification on massive load? azure function 重试场景的单元测试用例 - nodejs - azure function unit test cases for retry scenario - nodejs 如何在 lambda 函数中查看上下文变量 - How to see context variable in lambda function 如何用flutter触发Azure function - How to trigger Azure function with flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM