如何对 Azure Webjobs V3 进行集成测试?

How to do Integration Tests for Azure Webjobs V3?

Azure Webjob 现在是 V3,所以这个答案不再是最新的 ()

我想我们需要做这样的事情:

            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            using (host)
            {
                var jobHost = host.Services.GetService(typeof(IJobHost)) as JobHost;
                var arguments = new Dictionary<string, object>
                {
                    // parameters of MyQueueTriggerMethodAsync
                };

                await host.StartAsync();
                await jobHost.CallAsync("MyQueueTriggerMethodAsync", arguments);
                await host.StopAsync();
            }

队列触发器函数

    public MyService(
        ILogger<MyService> logger
    )
    {
        _logger = logger;
    }

    public async Task MyQueueTriggerMethodAsync(
        [QueueTrigger("MyQueue")] MyObj obj
    )
    {
        _logger.Log("ReadFromQueueAsync success");
    }

但是在那之后,我怎么才能看到发生了什么?

您有什么建议可以对 Azure Webjobs V3 进行集成测试?

我猜这是一个十字架 post with Github。产品团队建议查看他们自己的端到端测试,以获取有关如何处理集成测试的想法。

总结一下:

can configure 一个 IHost 作为 TestHost 并将您的集成服务添加到其中。

public TestFixture()
{
     IHost host = new HostBuilder()
         .ConfigureDefaultTestHost<TestFixture>(b =>
         {
              b.AddAzureStorage();
         })
         .Build();

         var provider = host.Services.GetService<StorageAccountProvider>();
         StorageAccount = provider.GetHost().SdkObject;
}

测试看起来像这样:

/// <summary>
/// Covers:
/// - queue binding to custom object
/// - queue trigger
/// - table writing
/// </summary>
public static void QueueToICollectorAndQueue(
    [QueueTrigger(TestQueueNameEtag)] CustomObject e2equeue,
    [Table(TableName)] ICollector<ITableEntity> table,
    [Queue(TestQueueName)] out CustomObject output)
{
    const string tableKeys = "testETag";

    DynamicTableEntity result = new DynamicTableEntity
    {
        PartitionKey = tableKeys,
        RowKey = tableKeys,
        Properties = new Dictionary<string, EntityProperty>()
        {
            { "Text", new EntityProperty("before") },
            { "Number", new EntityProperty("1") }
        }
    };

    table.Add(result);

    result.Properties["Text"] = new EntityProperty("after");
    result.ETag = "*";
    table.Add(result);

    output = e2equeue;
}

设置特定测试的难度取决于您使用的触发器和输出以及是否使用模拟器。