簡體   English   中英

如何對Azure Webjobs V3進行集成測試?

[英]How to do Integration Tests for Azure Webjobs V3?

Azure Webjob現在在V3上,因此該答案不再是最新的( 如何集成測試Azure Web Jobs?

我想我們需要做這樣的事情:

            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進行集成測試嗎?

我猜這是與Github的交叉發布。 產品團隊建議查看自己的端到端測試,以獲取有關如何處理集成測試的想法。

總結一下:

可以將 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;
}

設置特定測試的難度取決於您所使用的觸發器和輸出以及是否使用仿真器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM