#ServerlessTips - Azure Functions Author: Steef-Jan Wiggers Azure MVP

Azure Functions are small pieces of code you can run in the cloud. Even with these little pieces of code, you should create unit tests.

Since it is a small piece of code, you need to test creating a unit test, which is not too difficult or time-consuming.

In Visual Studio you can easily add a test project to your function. Note that for version 2.0 you need to have the target framework set to .NET Standard 2.0, which supports .NET Core 2.0 (your function code for Azure Functions Version 2.0) and the .NET Framework 4.6.1 and later versions (test project), and Visual Studio 15.3 or up.

When you create a test project, you can test methods to a test class, which contains all the tests (methods) for your function (note that you need to set a reference to your function project).

[TestClass] public class UnitTestConvertWindSpeedData : FunctionTestHelper.FunctionTest { [TestMethod] public void CanConvertLowWindSpeed() { //Arrange var windSpeedRequest = new WindSpeedData { WindSpeed = 8.0, Beaufort = 0, Location = "Amsterdam" }; //Act var query = new Dictionary<String, StringValues>(); var body = JsonConvert.SerializeObject(windSpeedRequest, Formatting.Indented); var result = ConvertWindSpeedToBeaufort.Run(HttpRequestSetup(query, body), log); var resultObject = (OkObjectResult)result; //Assert var resultResponse = new WindSpeedData { WindSpeed = 8.0, Beaufort = 5, Location = "Amsterdam" }; var resultBody = JsonConvert.SerializeObject(resultResponse, Formatting.Indented); Assert.AreEqual(resultBody, resultObject.Value); } }

Once you are satisfied with your unit tests, you can commit and push both your function and test projects to Azure DevOps. Next, you can create a build pipeline that includes your test projects – thus you can run your unit tests in the pipeline.

