Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test azure functions blob storage trigger option?

I have an azure function which is triggered when a zip file is uploaded to an azure blob storage container. I unzip the file in memory and process the contents and add/update the result into a database. While for the db part I can use the in memory db option. Somehow am not too sure how to simulate the blob trigger for unit testing this azure function.

All the official samples and some blogs mostly talk about Http triggers(mocking httprequest) and queue triggers (using IAsynCollection).

[FunctionName("AzureBlobTrigger")]
        public void Run([BlobTrigger("logprocessing/{name}", Connection = "AzureWebJobsStorage")]Stream blobStream, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {blobStream.Length} Bytes");
            //processing logic
        }
like image 523
Aravind Avatar asked Sep 16 '25 04:09

Aravind


1 Answers

There is a project about Unit test/ Integration test about azure function including blob trigger in github, please take a try at your side. Note that the unit test code is in FunctionApp.Tests folder.

Some code snippet about blob trigger from github:

unit test code of BlobFunction.cs

namespace FunctionApp.Tests
{
    public class BlobFunction : FunctionTest
    {
        [Fact]
        public async Task BlobFunction_ValidStreamAndName()
        {
            Stream s = new MemoryStream();
            using(StreamWriter sw = new StreamWriter(s))
            {
                await sw.WriteLineAsync("This is a test");
                BlobTrigger.Run(s, "testBlob", log);
            }
        }
    }
}
like image 187
Ivan Yang Avatar answered Sep 19 '25 15:09

Ivan Yang