Mock output binding Azure function python

Viewed 375

I want to create a unit test for my Azure Functions in Python using unittest. I am following the example in https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python#unit-testing , but I have added a blob output binding to my Http Trigger function, which I do not know how to mock when manually calling my function.

This is my function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ],
      "route" : "normalize"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "name": "todb",
      "type": "blob",
      "direction": "out",
      "path": "carga-db/web_{rand-guid}.csv",
      "connection": "bbdstorage_STORAGE"
    }
  ]
}

And this is my TestCase class:

import unittest

import azure.functions as func
from web_api import main

class TestFunction(unittest.TestCase):
    def test_my_function(self):
        # Construct a mock HTTP request.
        req = func.HttpRequest(
            method='POST',
            body=None,
            url='/api/normalize')

        # Call the function.
        resp = main(req, #what to put here?)

        # Check the output.
        self.assertEqual(
            resp.get_body(),
            b'Hello Test',
        )
0 Answers
Related