Open In App

Microsoft Azure – Testing Azure Functions

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the ways of testing different Azure Functions. This includes, for example, running tests of HTTP trigger functions using Postman and using Microsoft Storage Explorer to test Azure Blob triggers, Queue triggers, and other storage-service-related triggers. 

Prerequisites: Install the following tools, if you haven’t already done so:

  • Postman: You can download this from here.
  • Microsoft Azure Storage Explorer: You can download this from here

You can use Storage Explorer to connect to your storage accounts and view all the data available from different storage services, such as Blobs, Queues, Tables, and Files. You can also create, update, and delete them right from Storage Explorer.

Testing HTTP Triggers using Postman:

Create an HTTP trigger function that accepts the Firstname and Lastname parameters and sends them in the response. Once it is created, make sure you set Authorization Level as Anonymous.

Replace the default code with the following. Note that for the sake of simplicity, I have removed the validations. In real-time applications, you need to validate each and every input parameter:

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req,
                                           ILogger log)
{
   log.LogInformation(
       "C# HTTP trigger function processed a request.");
   string firstname = req.Query["firstname"];
   string lastname = req.Query["lastname"];
   string requestBody
       = await new StreamReader(req.Body).ReadToEndAsync();
   dynamic data
       = JsonConvert.DeserializeObject(requestBody);
   firstname = firstname ? ? data ?.firstname;
   lastname = lastname ? ? data ?.lastname;

   return (ActionResult) new OkObjectResult(
       $ "Hello, {firstname + " " + lastname}");
}

Open the Postman tool and complete the following:

The first step is to choose the type of HTTP method with which you would like to make the HTTP request. As our function accepts most of the methods by default, choose the GET method, shown as follows:
 

 

The next step is to provide the URL of the HTTP trigger. Note that you would need to replace <HttpTriggerTestUsingPostman> with your actual HttpTrigger function name, shown as follows:
 

 

Click on the Send button to make the request. If you have provided all the details expected by the API, then you would see a Status: 200 OK, along with the response, as shown here:
 

 


Last Updated : 03 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads