Skip to content

Azure-Samples/azure-functions-code-testing-sample

Repository files navigation

Azure Functions in-process model code testing sample

Unit and integration tests examples for Azure Functions in-process model.

This repo is a sample .NET 6 "Create Notes" API written using Azure Functions template and contains examples on how to properly write unit and integration tests for the Azure Functions classes.

Features

This project framework provides the following features:

  • Unit tests for Azure Function classes
  • Integration tests for Azure Function classes

Getting Started

Prerequisites

The code in this repo requires knowledge of the following concepts and frameworks:

You also need to have Docker Desktop installed in your local machine.

Installation

The API uses Postgresql database management system to store the created Notes.

To run an instance locally with docker you must run your API against a local instance of Postgresql, by simply typing the following command:

docker run --name postgresql -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres

To access the Postgresql instance through a management interface you can also run the following docker container:

docker run --name my-pgadmin -p 82:80 -e 'PGADMIN_DEFAULT_EMAIL=<your-email-placeholder>' -e 'PGADMIN_DEFAULT_PASSWORD=root'-d dpage/pgadmin4

And then access it via http://localhost:82 from a browser in your local machine.

Quickstart

To be able to run this demo on your own in your local machine you need to create a local.settings.json file inside the Fta.DemoFunc.Api project.

As it’s not advisable to store keys and secrets inside a git repository, for local development you can use a local.settings.json file to store configuration.

Sample local.settings.json file:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "NotificationApiUrl": "https://63cacb2d4f53a004202b1df7.mockapi.io/api/v1/",
  "ConnectionStrings": {
    "Database": "Server=localhost;Port=5432;Database=notesdb;User ID=postgres;Password=postgres;"
  }
}

The "NotificationApiUrl" setting is a mock 3rd party API (created with mockapi.io) which we call to send notification events that a new note has been created into our system.

You can use the "NotificationApiUrl" setting as is to run the demo API locally. It is here to demonstrate how to handle integration tests against 3rd party APIs that you do not control.

About

Unit and integration tests examples for Azure Functions.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages