Skip to content

Unit Testing Guide

Elvis Pranskevichus edited this page Jan 30, 2019 · 13 revisions

Unit Testing Guide

For most bindings it is possible to create a mock input object by creating an instance of an appropriate class from the azure.functions package.

For example, below is a mock test of an HttpTrigger function:

myapp/__init__.py:

import azure.functions as func

def my_function(req: func.HttpRequest) -> func.HttpResponse:
    name = req.params.get('name')
    if not name:
        name = 'Incognito'
    return func.HttpResponse(f"Hello {name}!")

myapp/test.py:

import unittest

import azure.functions as func
from . import my_function

def test_my_function():
    # Construct a mock HTTP request.
    req = func.HttpRequest(
        method='GET', 
        url='/my_function', 
        params={'name': 'Test'})

    # Call the function.
    resp = my_function(req)

    # Check the output.
    unittest.assertEqual(
        resp.get_body(), 
        'Hello, Test!',
    )