简体   繁体   English

如何为 Durable Azure 函数编写单元测试?

[英]How to write unit tests for Durable Azure Functions?

I'm writing an Azure Durable Function, and I would like to write some unit tests for this whole Azure Function. I'm writing an Azure Durable Function, and I would like to write some unit tests for this whole Azure Function.

I tried to trigger the Client function (the "Start" function, as it is often called), but I can't make it work.我试图触发客户端 function(通常称为“开始”function),但我无法使其工作。

I'm doing this for two reasons:我这样做有两个原因:

  1. It's frustrating to run the Azure Function code by running "func host start" (or pressing F5), then going to my browser, finding the right tab, going to http://localhost:7071/api/orchestrators/FooOrchestrator and going back to VS Code to debug my code. It's frustrating to run the Azure Function code by running "func host start" (or pressing F5), then going to my browser, finding the right tab, going to http://localhost:7071/api/orchestrators/FooOrchestrator and going back到 VS Code 来调试我的代码。
  2. I'd like to write some unit tests to ensure the quality of my project's code.我想编写一些单元测试来确保我的项目代码的质量。 Therefore I'm open to suggestions, maybe it would be easier to only test the execution of Activity functions.因此我愿意接受建议,也许只测试 Activity 函数的执行会更容易。

Client Function code客户端 Function 代码

This is the code of my Client function, mostly boilerplate code like this one这是我的客户 function 的代码,主要是像这样的样板代码

import logging
import azure.functions as func
import azure.durable_functions as df

async def main(req: func.HttpRequest, starter: str) -> func.HttpResponse:
    # 'starter' seems to contains the JSON data about
    # the URLs to monitor, stop, etc, the Durable Function
    client = df.DurableOrchestrationClient(starter)

    # The Client function knows which orchestrator to call
    # according to 'function_name'
    function_name = req.route_params["functionName"]

    # This part fails with a ClientConnectorError
    # with the message: "Cannot connect to host 127.0.0.1:17071 ssl:default"
    instance_id = await client.start_new(function_name, None, None)

    logging.info(f"Orchestration '{function_name}' starter with ID = '{instance_id}'.")

    return client.create_check_status_response(req, instance_id)

Unit test try单元测试尝试

Then I tried to write some code to trigger this Client function like I did for some "classic" Azure Functions:然后我尝试编写一些代码来触发这个客户端 function 就像我为一些“经典” Azure 函数所做的那样:

import asyncio
import json

if __name__ == "__main__":
    # Build a simple request to trigger the Client function
    req = func.HttpRequest(
        method="GET",
        body=None,
        url="don't care?",
        # What orchestrator do you want to trigger?
        route_params={"functionName": "FooOrchestrator"},
    )

    # I copy pasted the data that I obtained when I ran the Durable Function
    # with "func host start"
    starter = {
        "taskHubName": "TestHubName",
        "creationUrls": {
            "createNewInstancePostUri": "http://localhost:7071/runtime/webhooks/durabletask/orchestrators/{functionName}[/{instanceId}]?code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
            "createAndWaitOnNewInstancePostUri": "http://localhost:7071/runtime/webhooks/durabletask/orchestrators/{functionName}[/{instanceId}]?timeout={timeoutInSeconds}&pollingInterval={intervalInSeconds}&code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
        },
        "managementUrls": {
            "id": "INSTANCEID",
            "statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/INSTANCEID?taskHub=TestHubName&connection=Storage&code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
            "sendEventPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/INSTANCEID/raiseEvent/{eventName}?taskHub=TestHubName&connection=Storage&code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
            "terminatePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/INSTANCEID/terminate?reason={text}&taskHub=TestHubName&connection=Storage&code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
            "rewindPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/INSTANCEID/rewind?reason={text}&taskHub=TestHubName&connection=Storage&code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
            "purgeHistoryDeleteUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/INSTANCEID?taskHub=TestHubName&connection=Storage&code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
            "restartPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/INSTANCEID/restart?taskHub=TestHubName&connection=Storage&code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
        },
        "baseUrl": "http://localhost:7071/runtime/webhooks/durabletask",
        "requiredQueryStringParameters": "code=aakw1DfReOkYCTFMdKPaA1Q6bSfnHZ/0lzvKsS6MVXCJdp4zhHKDJA==",
        "rpcBaseUrl": "http://127.0.0.1:17071/durabletask/",
    }

    # I need to use async methods because the "main" of the Client
    # uses async.
    reponse = asyncio.get_event_loop().run_until_complete(
        main(req, starter=json.dumps(starter))
    )

But unfortunately the Client function still fails in the await client.start_new(function_name, None, None) part.但不幸的是,客户端 function 在await client.start_new(function_name, None, None)部分仍然失败。

How could I write some unit tests for my Durable Azure Function in Python?如何在 Python 中为我的 Durable Azure Function 编写一些单元测试?

Technical information技术信息

  • Python version: 3.9 Python 版本:3.9
  • Azure Functions Core Tools version 4.0.3971 Azure 功能核心工具版本 4.0.3971
  • Function Runtime Version: 4.0.1.16815 Function 运行时版本:4.0.1.16815

Not sure if this will help which is the official documentation from Microsoft on the Unit testing for what you are looking for - https://github.com/kemurayama/durable-functions-for-python-unittest-sample不确定这是否会有所帮助,这是 Microsoft 关于您正在寻找的单元测试的官方文档 - https://github.com/kemurayama/durable-functions-for-python-unittest-sample

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

相关问题 如何为GitPython克隆/拉取函数编写单元测试? - How to write unit tests for GitPython clone/pull functions? Python 3 如何为模块中的外部函数编写单元测试 - Python 3 how to write unit tests for try except outside functions in modules 将 Azure Durable Function 与 CosmosDBTrigger 结合使用 - Using Azure Durable Functions with a CosmosDBTrigger 如何将列表发送到 Azure Durable Functions - How do I send a list to Azure Durable Functions 如何为 python 中的多个并行运行 Azure 持久功能设置计时器? - How set timer for multiple parallel running Azure durable functions in python? azure function 应用程序 - 如何禁用耐用的许多功能 - azure function app - how to disable many functions in a durable 编写仅测试函数被调用的单元测试是否有意义? - Does it make sense to write unit tests that are just testing if functions are called? Azure 持久功能:Http 触发器错误 - Azure Durable Functions : Http Trigger error Azure Python 中的持久功能:为什么没有触发 function 2? - Azure Durable Functions in Python: why is function 2 not triggered? Azure 功能:Python:单元测试:使用 unittest 模拟应用程序设置 - Azure Functions: Python: Unit Tests: Mock Application Settings with unittest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM