Python v2编程模型下无法对Azure Durable Function进行单元测试
Python Azure Durable Function单元测试报错:TypeError: object NoneType can't be used in 'await' expression
我在为Python中的Azure Durable Function编写单元测试时遇到了大麻烦,具体情况如下:
函数代码
@bp.timer_trigger(schedule="0 0 12 * * *", arg_name="timer") @bp.durable_client_input(client_name="client") async def my_func(timer: func.TimerRequest, client: df.DurableOrchestrationClient) -> None: await client.start_new("durable_orchestrator")
测试代码
@pytest.mark.asyncio async def test_extract_data_timer(): mock_timer = create_autospec(func.TimerRequest, instance=True) mock_timer.past_due = False mock_starter = AsyncMock(spec=df.DurableOrchestrationClient) mock_starter.start_new = AsyncMock(return_value="test") result = await my_func(mock_timer, mock_starter) mock_starter.start_new.assert_called_with("durable_orchestrator")
(注:原测试代码存在变量名笔误,已修正mock_timer_request为mock_timer)
报错信息
E TypeError: object NoneType can't be used in 'await' expression
关键现象
移除@bp.timer_trigger和@bp.durable_client_input两个装饰器后,测试可以正常通过。同时我注意到函数签名的变化:
- 移除装饰器时的签名:
def my_func( timer: TimerRequest, starter: DurableOrchestrationClient ) -> Coroutine[Any, Any, None]
- 添加装饰器后的签名:
async def my_func( timer: TimerRequest, starter: DurableOrchestrationClient ) -> None
更新
目前Azure Durable Function的单元测试存在长期未解决的问题,相关问题已开放9个月,暂不清楚Azure团队的解决计划。
内容的提问来源于stack exchange,提问作者Jan Swart
相关产品推荐
相关产品推荐

