Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何模拟用Python编写的Azure函数中使用的对象_Python_Azure_Unit Testing_Dependency Injection_Azure Functions - Fatal编程技术网

如何模拟用Python编写的Azure函数中使用的对象

如何模拟用Python编写的Azure函数中使用的对象,python,azure,unit-testing,dependency-injection,azure-functions,Python,Azure,Unit Testing,Dependency Injection,Azure Functions,我找到了有关如何向用C#编写的Azure函数添加自定义绑定以及如何模拟这些函数的资源,但是,我还没有找到Python中的解决方案 要描述我的用例: 我有一个http触发函数,它创建到SQL数据库的连接,检索一些值并返回它们,即一个简单的GET。数据库连接包装在一个对象中,该对象提供了执行SELECT查询的方法 现在,该数据库是一个无服务器的Azure SQL数据库。我希望避免在手动测试或单元测试中实际查询它。我该怎么做?有没有办法将模拟注入Azure函数体中 到目前为止,我唯一能想到的是: de

我找到了有关如何向用C#编写的Azure函数添加自定义绑定以及如何模拟这些函数的资源,但是,我还没有找到Python中的解决方案

要描述我的用例:

我有一个http触发函数,它创建到SQL数据库的连接,检索一些值并返回它们,即一个简单的GET。数据库连接包装在一个对象中,该对象提供了执行SELECT查询的方法

现在,该数据库是一个无服务器的Azure SQL数据库。我希望避免在手动测试或单元测试中实际查询它。我该怎么做?有没有办法将模拟注入Azure函数体中

到目前为止,我唯一能想到的是:

def main(req: func.HttpRequest) -> func.HttpResponse:
    if req.headers.get("is-test", False):
        database = Database()
    else:
        database = MockDatabase()

    ...

这是我真正想要避免的事情。

我想我有一个解决方案,它对单元测试非常有用,但对本地部署没有任何作用

__初始值:

def main(req: func.HttpRequest) -> func.HttpResponse:
    wrapper = MyFunctionWrap(Database())
    return wrapper.exec(req)
myfunctionwrap.py:

class MyFunctionWrap:

    def __init__(self, dependency: Dependency):
        self._dependency = dependency

    def exec(self, req: func.HttpRequest) -> func.HttpResponse:
        ...
test_myfunction.py:

class TestFunction(unittest.TestCase):

    def test_my_function(self):
        req = func.HttpRequest(...)

        database = MockDatabase()
        wrapper = MyFunctionWrap(database)
        resp = wrapper.exec(req)

        ...

我认为我有一个解决方案,它对单元测试非常有用,但对本地部署却无能为力

__初始值:

def main(req: func.HttpRequest) -> func.HttpResponse:
    wrapper = MyFunctionWrap(Database())
    return wrapper.exec(req)
myfunctionwrap.py:

class MyFunctionWrap:

    def __init__(self, dependency: Dependency):
        self._dependency = dependency

    def exec(self, req: func.HttpRequest) -> func.HttpResponse:
        ...
test_myfunction.py:

class TestFunction(unittest.TestCase):

    def test_my_function(self):
        req = func.HttpRequest(...)

        database = MockDatabase()
        wrapper = MyFunctionWrap(database)
        resp = wrapper.exec(req)

        ...