使用 ServiceBusTrigger 对 Azure Function 进行单元和集成测试
Unit and Integration Test for Azure Function with ServiceBusTrigger
我有一个由 Azure 服务总线队列.
触发的 Azure 函数
函数如下
- 如何对这个 运行 方法进行单元测试?
- 以及如何通过从
AddContact
触发器开始,检查方法中的逻辑以及使用输出绑定发送到 blob 的数据来完成集成测试?
public static class AddContactFunction
{
[FunctionName("AddContactFunction")]
public static void Run([ServiceBusTrigger("AddContact", Connection = "AddContactFunctionConnectionString")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
}
How this Run method can be unit tested?
该方法是静态 public 方法。您可以通过调用静态方法 AddContactFunction.Run(/* parameters /*)
对其进行单元测试;您不需要服务总线命名空间或消息,因为您的函数希望从 SDK 接收字符串。您可以提供并验证逻辑是否按预期工作。
And how an integration test can be done by starting with AddContact trigger, checking the logic in the method and the data being sent to a blob using the output binding?
这将是一个复杂得多的场景。这将需要 运行 函数 运行 时间并生成一个真正的服务总线消息来触发函数以及验证 blob 是否已写入。 Functions 没有附带 integration/end-to-end 测试框架,您需要想出一些自定义的东西。 Azure Functions Core Tools 可能有助于实现这一目标。
我也有同样的疑虑。
添加单元测试并不太复杂,归根结底,它是一个函数,所以我们要做的就是使用正确的字符串调用 Azure 函数,参数为 string myQueueItem
.
添加集成测试需要一些额外的基础工作。在 Github project, the author uses the TestFunctionHost
class from Azure/azure-functions-host 项目中。
我尝试遵循这个策略,但设置所有这些所需的代码量对我来说高得令人不安。没有很多是有据可查的,有些东西需要开发人员使用 Azure App Services myGet feed.
我想要一种更简单的方法,幸好我找到了。
Azure Functions 建立在 Azure WebJobs SDK package, and leverages its JobHost class 到 运行 之上。所以在我们的集成测试中,我们需要做的就是设置这个主机,并告诉它在哪里寻找要加载的 Azure Functions 和 运行.
IHost host = new HostBuilder()
.ConfigureWebJobs()
.ConfigureDefaultTestHost<CLASS_CONTAINING_THE_AZURE_FUNCTIONS>(webjobsBuilder => {
webjobsBuilder.AddAzureStorage();
webjobsBuilder.AddServiceBus();
})
.ConfigureServices(services => {
services.AddSingleton<INameResolver>(resolver);
})
.Build();
using (host) {
await host.StartAsync();
// ..
}
...
完成后,我们可以将消息发送到 ServiceBus,我们的 Azure Functions 将被触发。曾经甚至可以在正在测试和调试问题的函数中设置断点!
我有 blogged about the whole process here and I have also created a github repository at this link 来展示 Azure Functions 的测试驱动开发。
我有一个由 Azure 服务总线队列.
触发的 Azure 函数函数如下
- 如何对这个 运行 方法进行单元测试?
- 以及如何通过从
AddContact
触发器开始,检查方法中的逻辑以及使用输出绑定发送到 blob 的数据来完成集成测试?
public static class AddContactFunction
{
[FunctionName("AddContactFunction")]
public static void Run([ServiceBusTrigger("AddContact", Connection = "AddContactFunctionConnectionString")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
}
How this Run method can be unit tested?
该方法是静态 public 方法。您可以通过调用静态方法 AddContactFunction.Run(/* parameters /*)
对其进行单元测试;您不需要服务总线命名空间或消息,因为您的函数希望从 SDK 接收字符串。您可以提供并验证逻辑是否按预期工作。
And how an integration test can be done by starting with AddContact trigger, checking the logic in the method and the data being sent to a blob using the output binding?
这将是一个复杂得多的场景。这将需要 运行 函数 运行 时间并生成一个真正的服务总线消息来触发函数以及验证 blob 是否已写入。 Functions 没有附带 integration/end-to-end 测试框架,您需要想出一些自定义的东西。 Azure Functions Core Tools 可能有助于实现这一目标。
我也有同样的疑虑。
添加单元测试并不太复杂,归根结底,它是一个函数,所以我们要做的就是使用正确的字符串调用 Azure 函数,参数为 string myQueueItem
.
添加集成测试需要一些额外的基础工作。在 Github project, the author uses the TestFunctionHost
class from Azure/azure-functions-host 项目中。
我尝试遵循这个策略,但设置所有这些所需的代码量对我来说高得令人不安。没有很多是有据可查的,有些东西需要开发人员使用 Azure App Services myGet feed.
我想要一种更简单的方法,幸好我找到了。 Azure Functions 建立在 Azure WebJobs SDK package, and leverages its JobHost class 到 运行 之上。所以在我们的集成测试中,我们需要做的就是设置这个主机,并告诉它在哪里寻找要加载的 Azure Functions 和 运行.
IHost host = new HostBuilder()
.ConfigureWebJobs()
.ConfigureDefaultTestHost<CLASS_CONTAINING_THE_AZURE_FUNCTIONS>(webjobsBuilder => {
webjobsBuilder.AddAzureStorage();
webjobsBuilder.AddServiceBus();
})
.ConfigureServices(services => {
services.AddSingleton<INameResolver>(resolver);
})
.Build();
using (host) {
await host.StartAsync();
// ..
}
...
完成后,我们可以将消息发送到 ServiceBus,我们的 Azure Functions 将被触发。曾经甚至可以在正在测试和调试问题的函数中设置断点!
我有 blogged about the whole process here and I have also created a github repository at this link 来展示 Azure Functions 的测试驱动开发。