Search code examples
c#.net-coreintegration-testingazure-webjobs

How to do Integration Tests for Azure Webjobs V3?


Azure Webjob is now on V3, so this answer is not up to date anymore (How to integration test Azure Web Jobs?)

I imagine we need to do something like this:

            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();
            }

QueueTrigger Function

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

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

But after that, how can I see what's happened?

What do you suggest to be able to do Integration Tests for Azure Webjobs V3?


Solution

  • I'm guessing this is a cross post with Github. The product team recommends looking at their own end-to-end testing for ideas on how to handle integration testing.

    To summarize:

    You can configure an IHost as a TestHost and add your integrated services to it.

    public TestFixture()
    {
         IHost host = new HostBuilder()
             .ConfigureDefaultTestHost<TestFixture>(b =>
             {
                  b.AddAzureStorage();
             })
             .Build();
    
             var provider = host.Services.GetService<StorageAccountProvider>();
             StorageAccount = provider.GetHost().SdkObject;
    }
    

    Tests would look something like this:

    /// <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;
    }
    

    The difficulty in setting up a specific test depends on which triggers and outputs you are using and whether or not an emulator.